Understanding VB .NET Error Handling Structure


.NET uses the Try..Catch..Finally structure. You can nest these structures, which can handle multiple types of errors with ease. The structure looks like the following:

 Private Sub Example()    Try        'Some code    Catch        'Code to handle the error    Finally        'Cleanup code here this always run, regardless of whether an error        'occurs    End Try End Sub 

The other form of this type of structure is identical except that there are multiple Catch blocks. This looks like the following:

 Private Sub Example()    Try       'Some code    Catch excNull as ArgumentNullException       'Code to handle this specific exception    Catch excZero as DivideByZeroException       'Code to handle a divide by zero error    Catch       'Code to handle any other errors    Finally       'Cleanup code here this always run, regardless of whether an error       'occurs    End Try End Sub 

There can be as many Catch blocks as needed. The Try block always runs; the Catch block only runs when there is an error, and the Finally block always runs.

Tip

Any variables declared in the Try..Catch..Finally blocks are block-level variables. Therefore, if you declare a variable in the Try block, it will not be in scope in the Finally block. Any variable that must be accessed in a different block must be declared before the Try block.




Building Client/Server Applications with VB. NET(c) An Example-Driven Approach
Building Client/Server Applications Under VB .NET: An Example-Driven Approach
ISBN: 1590590708
EAN: 2147483647
Year: 2005
Pages: 148
Authors: Jeff Levinson

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