DEALING WITH ERRORS


As your Visual Basic programs grow larger and more complex, errors are going to happen. That is just the way things work. Even the most experienced programmers run into errors all the time. Visual Basic applications are subject to many different types of errors. The occurrence of an error causes an application to behave inappropriately and is often referred to as a bug. Your job as a programmer is to seek out and remove (or handle) these bugs.

There are many things that you can do to minimize the number of errors in your applications. For starters, you can sit down and plan out the overall design of your applications before you start writing them. Once created, you should test your applications extensively to look for bugs. Other good programming practices that you should consider include:

  • 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, objects, arrays, and Function and Sub procedures

  • Anticipating specific situations where errors might occur and trying to deal with them programmatically

There are three basic categories of errors that you will experience when developing Visual Basic applications. These are:

  • Syntax errors

  • Logical errors

  • Run-time errors

Syntax Errors

The most common type of error is a syntax error. A syntax error occurs when you key in a statement that does not conform to Visual Basic's rules. Syntax errors are often the result of typos, such as when you accidentally mistype a keyword or leave out a required parameter.

Visual Basic identifies syntax errors in your programming statements as you finish typing them by underlining the errors, as demonstrated in Figure 11.4. If you move the pointer over a syntax error, a ToolTip will display a brief error message.

image from book
Figure 11.4: The Compiler flags Visual Basic syntax errors by underlining them with a jagged blue line.

image from book
DEFINITION

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

image from book

Because syntax errors are identified for you as you key in your Visual Basic program statements, they are easy to locate and correct. You can also view syntax errors from the Error List window, as shown at the bottom of Figure 11.4. This window will automatically open during application development if you attempt to run a Visual Basic application that has a syntax error in it. The window shows each error, provides a brief description of the error, and identifies the line number of the statement that contains the error. If you want, you can double-click on an error listed in the Error List window and Visual Basic will respond by locating the statement that contains it in the code editor.

Trick 

Sometimes you might miss when Visual Basic flags a syntax error in your code. To help prevent this from happening, you might want to keep the Error List window open while you are writing code. You can do this by clicking on the View menu and then selecting the Error List menu item.

Logical Errors

Logical errors are errors caused by a mistake on the programmer's part. For example, you might accidentally add together two numbers that should have been subtracted. The end result is that everything runs fine but the output is not what you expect. Another example of a logical error is when you accidentally set up an endless loop.

Visual Basic won't be able to flag or report your logical errors. The best way to deal with logical errors is to try and prevent them in the first place by taking extra care in the formulation of your programming logic. Organize your Visual Basic applications into procedures by assigning each procedure one specific task to perform. Then do your best to test out each procedure as you go. However, even the best laid plans often go awry, and when this occurs, there is no substitute for careful testing as a means of identifying logical errors.

image from book
DEFINITION

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

image from book

Run-Time Errors

A third category of errors is the run-time error, which occurs whenever a statement attempts to do something that is not possible. For example, a run-time error will occur if an application attempts to access a disk drive that does not exist or if a broken network connection prevents the access of a network file.

Unlike syntax errors, the compiler won't flag runtime errors for you. In fact, if a seldom-used procedure contains code that might produce a run-time error and you don't carefully test the functionality provided by that procedure, you may not catch the run-time error at all, in which case, the user will be left to discover it.

image from book
DEFINITION

A run-time error occurs when an application attempts to perform an illegal action.

image from book

If you come across a run-time error when running your Visual Basic application in debug mode, you'll receive an error message like the one shown in Figure 11.5. However, if you don't catch the run-time error during testing and development, your application may or may not display an error message when its release version is being executed. Worse still, your application may simply lock up and stop responding.

image from book
Figure 11.5: An example of a Visual Basic runtime error displayed when an application is being executed In design mode.

A run-time error can also occur if the user passes your application some unanticipated input. Therefore, it is important that you add logic to your applications to validate, as much as possible, all user input. It is also critical to test all of the functionality provided by your Visual Basic applications as thoroughly as possible in order to minimize the possible of running into unexpected run-time errors. However, try as you may, there is no way to completely avoid run-time errors. For example, sometimes hardware just fails or the network goes down. If your application requires these resources to execute, then run-time errors will eventually occur. However, as you'll learn later in this chapter, Visual Basic provides you with the ability to develop exception handlers so that you can recover from or at least gracefully respond to unavoidable run-time errors.




Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
ISBN: 1592008143
EAN: 2147483647
Year: 2006
Pages: 126

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