Global Exception Handlers


Exceptions bubble up to the parent method. For example, in the following console application, Sub Main has an exception handler and calls the function DivideByZero. When DivideByZero causes an exception, because the function itself has no exception handler, the exception bubbles up to the method that called it—the parent method Sub Main.

Sub Main()
Try
’Try dividing 1 by 0
DivideByZero(1)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Function DivideByZero(ByVal MyNumber As Integer) _
As Integer
’Return the input divided by zero
’This will always cause an exception
Return MyNumber / 0
End Function

Putting an exception handler in Sub Main gives the effect of a global exception handler. If an uncaught exception occurs, the global error handler will catch the error—at least, this is the theory. Unfortunately, this works only 95 percent of the time. Because of a problem in Visual Basic .NET 2002 and Visual Basic .NET 2003, some types of exceptions—typically, COM interop– based exceptions—do not bubble up to the parent exception handler (for example, an exception in the Visual Basic 6 Treeview ActiveX control’s, ClickEvent event). However, this is still a valuable technique and one worth implementing in your applications.

Add a global exception handler to the employee management system

In this exercise, you will add a global exception handler to the employee management system. Because this is a Windows application, the Windows Forms Application object is available to the application. The Application object exposes a ThreadException event, which allows you to add an even finer degree of global exception handling than Try…Catch exception handling.

  1. In Visual Basic .NET, open the solution CH08_ErrorHandling\Start\EMS\Start\EMS.sln.

  2. Open frmDashboard.vb, and at the top of the frmDashboard_Load event, add the following line of code:

    Err.Raise(1, , "My Error")
  3. Press F5 to run the employee management system. Log on using a username of RKing and the password RKing. As soon as the dashboard opens, an exception will be thrown and cause an unhandled exception in the code, as shown here:

    click to expand

  4. Now you will add a global exception handler. Open MainModule.vb, and change Sub Main to

    Sub Main()
    AddHandler Application.ThreadException, _
    AddressOf MyGlobalExceptionHandler
    ’Open the login form
    Dim Login As New frmLogin()
    Login.ShowDialog()
    ’When login form closes, if OK was pressed,
    ‘then open the dashboard
    ’If cancel was pressed then End
    If G_OK = True Then
    Dim Dashboard As New frmDashboard()
    Dashboard.ShowDialog()
    Else
    End
    End If
    End Sub

  5. Add the following procedure after Sub Main:

    Sub MyGlobalExceptionHandler(ByVal sender As Object, _
    ByVal e As System.Threading.ThreadExceptionEventArgs)
    EventLog.LogException(e.Exception)
    If MsgBox("An exception occured. Do you want to continue?", _
    MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton1) = _
    MsgBoxResult.No Then
    End
    End If
    End Sub

  6. Press F5 to run the application. Now after logging on, when the exception occurs, the application logs it to the event log and prompts you to continue, as shown here:

  7. Before continuing, be sure to remove the line of code in the frmDashboard.vb frmDashboard_Load event that reads Err.Raise(1, , “My Error”).

    To see the global exception handler in action, log on to the employee management system, click View or change personal information on the dashboard, and then enter a bad first name such as Robert’ (including the single quote). The application will raise an exception when OK is clicked and the system tries and then fails to save the information back to the database.

start sidebar
Viewing the Event Log Remotely

A powerful feature of event logs is the ability to view them remotely. With this feature, when a user of your system reports an exception, assuming you have the right permissions, you can view the event log on the user’s computer. To do this, start the event viewer by choosing Run from the Start menu, typing EventVwr.exe, and pressing Enter. When the event viewer opens, choose the menu item Action | Connect To Another Computer. The Select Computer dialog box will open, and you can choose a computer on the local network, as shown in Figure 8-1.

click to expand
Figure 8-1: View the event log on a user’s computer

Type the computer name, and click the OK button. The event log on the remote computer will open, and you can view any events your application has logged.

end sidebar




Security for Microsoft Visual Basic  .NET
Security for Microsoft Visual Basic .NET
ISBN: 735619190
EAN: N/A
Year: 2003
Pages: 168

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