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.
In Visual Basic .NET, open the solution CH08_ErrorHandlingStartEMSStartEMS.sln.
Open frmDashboard.vb, and at the top of the frmDashboard_Load event, add the following line of code:
Err.Raise(1, , "My Error")
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:
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
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
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:
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.
18.119.138.202