Chapter 5. Variables

Types of Exception Handlers

When you run a project as a compiled program or component, untrapped exceptions are fatal they cause your program to terminate. You must make every effort to prevent this from happening. To prevent exceptions from stopping code execution (and terminating compiled programs), you create exception handlers to trap the exceptions. When an exception is trapped, Visual Basic doesn't display an error message or terminate the application. Instead, code that you've written to specifically handle the exception is executed.

Note

In previous editions of Visual Basic, error handlers were created using On Error statements. This method of handling errors was considered an unstructured approach. Although Visual Basic .NET still supports this method, it has been replaced with the new structured exception-handling construct Try Catch Finally. It might not make sense to attempt to convert all of the error handlers of existing code to use Try Catch Finally (although you can choose to do so), but you should use the new structured exception-handling mechanism for all new code. I do not discuss On Error in this book because it is considered an outdated methodology.


 

Microsoft Knowledge Base article Q301283 states, "A try-catch-finally block is a 'wrapper' that you put around any code where the possibility of an exception exists." This statement is misleading. Every single statement you write has a "possibility" of encountering an error. Therefore, all procedures should have an exception handler, regardless of the amount of code they contain.

It's best to place a Try statement as the first line of code, immediately after the procedure header and just after the variable declarations. If your variable declarations use other variables as initializers (for example, Dim intMyVariable As Integer = intAnotherVariable + 2), you should place these declarations within the Try structure. Be aware that exceptions can "bubble up" the call stack to exception handlers in procedures higher in the stack (as I'll discuss later in this chapter). If a procedure's exceptions are allowed to bubble up in this manner, you should clearly explain this behavior in a prominent comment at the top of the procedure.

Important

Variables declared within the Try block of an exception handler have block scope they aren't available to any Catch or Finally blocks, and they aren't available outside of the Try End Try construct. For this reason, it's best to leave declarations outside of the Try block unless the declaration has an inherent risk of throwing an exception.


 

You can create multiple exception handlers in a procedure by nesting Try Catch Finally blocks, but no more than one exception handler is active at a time. Visual Basic treats the handler identified by the most recent Try statement (discussed in the next section) as the enabled exception handler. It's often advantageous to switch exception handlers at different points within a procedure, as I'll also discuss in this chapter.



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

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