October 24, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The Book of Visual Studio .NET - Structured Exception Handling in VB

  • January 22, 2003
  • By Developer.com Staff
  • Send Email »
  • More Articles »

Basic Rules

Basic rules govern the use of a Try block. (They will quickly become obvious after using Try blocks a few times.)

  • All Try blocks must employ at least one catch or finally clause.

  • A Catch clause with no other parameters will catch all unhandled exceptions.

  • The finally clause always executes when available except when an Exit Tryoccurs. As such, the finally clause is a good place to perform componentcleanup. If an Exit Try statement is used anywhere in the Try block then itis better to perform cleanup after the End Try block statement.

  • Developers familiar with the On Error statement may still perform errorhandling as they did in Visual Basic only when a Try block does not exist inthe procedure.

Exception Handling Examples

For the following examples we will create a single Windows Form and add a buttonfor each example. To begin, create a new project and name it "ExceptionHandler".

Try...Catch

One popular and easy way to understand an example of error handling hasalways been the divide by zero error, or in .NET terms "exception." You will usethe divide by zero exception wherever possible so as to not distract you fromwhat the chapter is trying to convey. (You will examine a few other specificexception conditions later in this chapter.)

This example demonstrates the simplest of all exception structures:

1. Drag a button onto your Windows Form and label it "Try Catch".

2. Change the buttons name to "btnTryCatch".

3. Apply the following code in the click event of the button.

Dim intResult as Integer"Dim int1 as Integer = 5Dim int2 as Integer = 0Try  intResult = int1 / int2Catch objException as System.OverflowException  Messagebox.Show("Divide by zero has occurred")End Try

The preceding example evaluates the exception object by using the catchclause. The catch clause checks to see if the exception object contains an"OverflowException" exception. If so, and in this case it will, the code in thecatch clause executes.

The Finally Clause

This example employs the same code as the one previously, except it demonstratesthat the Finally clause always executes:

1. Drag another button onto your Windows Form and label it "Finally".

2. Change the buttons name to "btnFinally".

3. Apply the following code in the click event of the button.

Dim intResult as IntegerDim int1 as Integer = 5Dim int2 as Integer = 0Try  intResult = int1 / int2Catch objException as System.OverflowException  Messagebox.Show("Divide by zero exception has occurred")Finally  Dim obj As New System.Text.StringBuilder()  obj = obj.Append("Regardless of whether or not an ")  obj = obj.Append("exception occurs, the Finally clause ")  obj = obj.Append("will execute.")  MessageBox.Show(obj.ToString)  obj = NothingEnd Try

Feel free to remove the errant code with "intResult = int1 / 1" andobserve that the finally clause still executes.





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel