Chapter 7 -- Error Handling

[Previous] [Next]

Chapter 7

The grandest of intentions and the most thorough planning can't eliminate errors in code. Errors can be programmer errors (usually caused by bad assumptions, such as that a denominator will never be 0), environmental errors (such as an attempt to save a file that is too large for the amount of free space on a disk), or timing errors (such as trying to set the focus to a form that isn't fully loaded). You should strive for error-free code, but you should also create every procedure with the assumption that an error might occur. This means that every procedure must contain an error handler.

There are practically an infinite number of possible program errors, but they basically fall into two types: compile errors and run-time errors. A compile error is an error that prevents Microsoft Visual Basic's compiler from compiling the code; Visual Basic won't execute a procedure that has a compile error in it, and you cannot distribute a run-time version of an application that has a compile error. Most compile errors are a result of erroneous syntax.

For example, if you attempt to call a procedure defined as

  PublicSub  MyProcedure(intMyVariable  AsInteger  ) 

by using the statement below, a compile error occurs because of the added argument in the Call statement.

  Call  MyProcedure(intVariable1,intVariable2) 

Run-time errors occur while the program is running and are usually the result of trying to perform an invalid operation on a variable. For instance, the following code doesn't generate a compile error. (It's syntactically correct if intMyVariable is a declared variable.)

  Print  10/intMyVariable 

Under most circumstances, this code won't even generate a run-time error. However, if the value of intMyVariable is 0, Visual Basic halts with a run-time error because 10 can't be divided by 0. If a run-time error occurs when you run a project in the IDE, code execution stops at the offending line and an error message appears. In a compiled program, an unhandled error is fatal, causing the entire application to crash. You can prevent execution from stopping when run-time errors occur by creating error handlers.



Practical Standards for Microsoft Visual Basic
Practical Standards for Microsoft Visual Basic .NET (Pro-Developer)
ISBN: 0735613567
EAN: 2147483647
Year: 2000
Pages: 57

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