DEALING WITH ERRORS


Errors are part of the programming experience. As your programs become larger and more complex, errors become even more likely. Even the most experienced programmers encounter errors as they build applications. But as you gain experience in dealing with errors, you develop techniques for minimizing their impact.

Your application can be afflicted with many different types of errors. In programming terminology, these are often referred to as bugs. As a programmer, your goal is to find and remove these bugs.

You can employ several techniques to reduce the number of errors in your applications. One of the most useful is to have a plan before you begin coding. By figuring out how the code should work before starting, you are less likely to create a class, module, or function that behaves improperly.

Another technique is to test your application for bugs as you're coding it. Periodically compiling and even running your application in stages helps you spot bugs early, before they become obscured by the full bulk of your application.

Some other good programming practices to consider include these:

  • Creating a simple and easy-to-use interface

  • Providing users with clear instructions

  • Validating user input data before accepting it

  • Using consistent naming schemes for constants, variables, classes, objects, arrays, and functions

  • Thinking through the overall program flow and anticipating where and how errors might occur

There are three basic categories of errors that you will experience when developing Visual C++ applications:

  • Syntax and compiler errors

  • Logical errors

  • Runtime errors

Syntax Errors

The syntax error is probably the most prevalent type of programming error. It involves creating any statement that Visual C++ does not recognize. This can occur as a result of misspelling a function name or forgetting to include a semicolon at the end of a line.

Visual C++ identifies syntax errors when you attempt to compile your program. It generates the result of all errors in the Output window, as demonstrated in Figure 11.4. If you click on the error, Visual C++ responds by taking you to the offending section of code.

image from book
Figure 11.4: The Output window displays errors found at compile time.

Hint 

A syntax error is an error that occurs when you do not write a code statement according to the rules of the programming language.

Logical Errors

Logical errors are those errors that result from the difference between how a program actually works versus how it is supposed to work. For example, you might create a logical error by multiplying two numbers that should have been added. This might cause your application to generate an undesired or unexpected result. Another example of a logical error is when you accidentally set up an endless loop.

Hint 

A logical error is created when the programmer makes a mistake in implementing in code the logic used to perform a given task.

Visual C++ might generate warnings during compile time for some common logical errors. However, your best defense against them is trying to prevent them by carefully planning and testing your code. Organize your C++ applications into functions that are easy to read and only perform a limited number of related tasks. As you develop your application, test it periodically to see if it meets your expectations. This helps reduce logic errors because it makes them more apparent earlier in the development process.

Runtime Errors

A third category of errors is the runtime error, which occurs whenever a statement attempts to perform a task that is outside the bounds of its capabilities or simply impossible. For example, a runtime error occurs if an application attempts to divide one value by another that is zero. Or one might occur if your program attempts to access system resources, such as memory or disk space, that are unavailable.

Hint 

A runtime error occurs when an application attempts to perform an illegal action.

Runtime errors can be ticking time bombs in your code. In fact, if a seldom-used function contains code that might produce a runtime error and you don't carefully test the functionality provided by that function, you might not catch the runtime error. This leaves your user to discover it.

If you come across a runtime error when running your Visual C++ application in Debug mode, you receive an error message like the one shown in Figure 11.5. However, if you don't catch the runtime error during testing and development, your application might or might not display an error message when its release version is executed. Worse still, your application might simply lock up and stop responding.

image from book
Figure 11.5: An example of a visual C++ runtime error displayed when an application is being executed in Design mode.

A runtime error also might occur if the user gives your application incorrect or unexpected input, or if a user uses your application in unexpected ways. You can prevent these errors to some extent by adding logic within your applications that validates all data and limits certain risky tasks or activities. This gives you an important defense against runtime errors.

It can also help to have different people test your programs. Different users bring different perspectives and assumptions on how your application should be used. They might also be able to expose your application to different computer configurations, which might reveal flaws about the assumptions you made in terms of what resources would be available and how they would work.

Although it is critical to test your programs as thoroughly as possible, there is no way to completely avoid runtime errors. Your application might require memory or resources that suddenly aren't available. In such a situation, a runtime error is likely to occur. However, as you'll learn later in this chapter, Visual C++ allows you to develop exception handlers so that you can recover from or at least gracefully respond to unavoidable runtime errors.




Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
ISBN: 735615381
EAN: N/A
Year: 2005
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net