This chapter's project code will be somewhat brief. Error-handling code will appear throughout the entire application, but we'll add it in little by little as we craft the project. For now, let's just focus on the central error-handling routines that will take some basic action when an error occurs anywhere in the program.
General Error Handler
As important and precise as error handling needs to be, the typical business application will not encounter a large variety of error types. Applications like the Library Project are mainly vulnerable to three types of errors: (1) data entry errors; (2) errors that occur when reading data from, or writing data to, a database table; and (3) errors related to printing. Sure, there may be numeric overflow errors or other errors related to in-use data, but it's mostly interactions with external resources, such as the database, that concern us.
Because of the limited types of errors occurring in the application, it's possible to write a generic routine that informs the user of the error in a consistent manner. Each time a run-time error occurs, we will call this central routine, just to let the user know what's going on. The code block where the error occurred can then decide whether to take any special compensating action, or continue on as if no error occurred.
Load the "Chapter 9 (Before) Code" project, either through the New Project templates, or by accessing the project directly from the installation directory. To see the code in its final form, load "Chapter 9 (After) Code" instead.
In the project, open the General.vb class file, and add the following code as a new method to Module General.
Insert Chapter 9, Snippet Item 1.
Public Sub GeneralError(ByVal routineName As String, _ ByVal theError As System.Exception) ' ----- Report an error to the user. MsgBox("The following error occurred at location '" & _ routineName & "':" & vbCrLf & vbCrLf & _ theError.Message, _ MsgBoxStyle.OKOnly Or MsgBoxStyle.Exclamation, _ ProgramTitle) End Sub
Not much to that code, is there? So, here's how it works. When you encounter an error in some routine, the in-effect error handler calls the central GeneralError method.
Public Sub SomeRoutine() On Error GoTo ErrorHandler ' ----- Lots of code here. Return ErrorHandler: GeneralError("SomeRoutine", Err.GetException()) Resume Next End Sub
You can use it with structured errors as well.
Try ' ----- Troubling code here. Catch ex As System.Exception GeneralError("SomeRoutine", ex) End Try
The purpose of the GeneralError global method is simple: Communicate to the user that an error occurred, and then move on. It's meant to be simple, and it is simple. You could enhance the routine with some additional features. Logging of the error out to a file (or any other active log listener) might assist you later if you needed to examine application-generated errors. Add the following code to the routine, just after the MsgBox command, to record the exception.
Insert Chapter 9, Snippet Item 2.
Of course, if an error occurs while writing to the log, that would be a big problem, so add one more line to the start of the GeneralError routine.
Insert Chapter 9, Snippet Item 3.