The Try catch and Finally Keywords

Here is an example showing some typical, simple structured exception-handling code in Visual Basic 2010. In this case, the most likely source of an error is the iitems argument. If it has a value of zero, then this would lead to dividing by zero, which would generate an exception.

First, create a Windows Forms Application in Visual Basic 2010 and place a button on the default Forml created in the project. In the button's Click event, place the following two lines of code:

Dim sngAvg As Single sngAvg = GetAverage(0, 10 0)

Code snippet from ExceptionHandlingSampleCodeForm

Then put the following function in the form's code:

Private Function GetAverage(iItems As Integer, iTotal As Integer) as Single ' Code that might throw an exception is wrapped in a Try block Try

Dim sngAverage As Single

' This will cause an exception to be thrown if iitems = 0 sngAverage = CSng(iTotal \ iitems)

' This only executes if the line above generated no error MessageBox.Show("Calculation successful") Return sngAverage Catch excGeneric As Exception

' If the calculation failed, you get here

MessageBox.Show("Calculation unsuccessful - exception caught") Return 0 End Try End Function

Code snippet from ExceptionHandlingSampleCodeForm

This code traps all the exceptions with a single generic exception type, and doesn't include any Finally logic. Run the program and press the button. You will be able to follow the sequence better if you place a breakpoint at the top of the GetAverage function and step through the lines.

A breakpoint is a marker in a line of code indicating that you wish execution of the program to be suspended when execution reaches that line. When a breakpoint is reached, you have the opportunity to examine values of variables or perform other actions that may help you diagnose a problem. A breakpoint is set in Visual Studio by positioning the cursor on a line of code and choosing the appropriate option. Depending on your settings, you probably also have a shortcut key to set a breakpoint. The most common key used for that purpose is F9. You can also set a breakpoint by clicking next to the desired line in the vertical gray bar on the left side of the Code Editor window.

Here is a more complex example that traps the divide-by-zero exception explicitly. This second version of the GetAverage function (notice that the name is GetAverage2) also includes a Finally block:

Private Function GetAverage2(iItems As Integer, iTotal As Integer) as Single

' Code that might throw an exception is wrapped in a Try block Try

Dim sngAverage As Single

' This will cause an exception to be thrown. sngAverage = CSng(iTotal \ iItems)

' This only executes if the line above generated no error. MessageBox.Show("Calculation successful") Return sngAverage Catch excDivideByZero As DivideByZeroException

' You'll get here with an DivideByZeroException in the Try block MessageBox.Show("Calculation generated DivideByZero Exception") Return 0 Catch excGeneric As Exception

' You'll get here when any exception is thrown and not caught in ' a previous Catch block.

MessageBox.Show("Calculation failed - generic exception caught") Return 0 Finally

' Code in the Finally block will always run.

MessageBox.Show("You always get here, with or without an error") End Try

End Function

Code snippet from ExceptionHandlingSampleCodeForm

This code contains two Catch blocks for different types of exceptions. If an exception is generated, then .NET will go down the Catch blocks looking for a matching exception type. That means the Catch blocks should be arranged with specific types first and more generic types after.

Place the code for GetAverage2 in the form, and place another button on Form1. In the Click event for the second button, place the following code:

Dim sngAvg As Single sngAvg = GetAverage2(0, 100)

Code snippet from ExceptionHandlingSampleCodeForm

Run the program again and press the second button. As before, it's easier to follow if you set a breakpoint early in the code and then step through the code line by line.

0 0

Post a comment