We are in the process of writing a fairly big application and I am trying to design an error handling strategy.
Although the app is currently being written in VB6 we will be porting it over to .net in about a year or so. So we want to limit the work involved when we do.
I am interested in 2 types of errors. Those I plan for in the code (custom number 1002) e.g. missing date of birth, and those I have not planned for (custom number 1001) e.g. missing file, invalid use of null.
What we want to do is log an error in the database in the function/sub it occurs and then raise a custom error to the calling functions until the error is handled at an appropriate time, e.g. Button click event where an appropriate message is displayed to the user.
As there are any number of functions between the button click event and the function the error occured in some functions will only simply re-raise the error for its calling function to handle.
For this to work I need to know which function the error originated so the error is not logged twice and/or whether the error has been logged.
I have some code to demonstrate this.
In .net there are exception rather than errors. I really need an exception handling class to achieve this.Code:Option Explicit Private Sub Command1_Click() On Error Goto Abort func1 Exit Sub Abort: logerror err MsgBox err.Description, vbCritical, "An error occurred" End Sub Private Sub func1() On Error Goto Abort func2 Exit Sub Abort: If err.Number <> 1001 Then logerror err End If err.Raise err.Number, err.Description End Sub Private Sub func2() On Error Goto Abort func3 Exit Sub Abort: If err.Number <> 1001 Then logerror err End If err.Raise err.Number, err.Description End Sub Private Sub func3() On Error Goto Abort err.Raise 1002, , "Missing Date of Birth" Exit Sub Abort: logerror err If err.Number <> 1002 Then err.Raise 1001, "Unexpected Application Error" End If End Sub Private Sub logerror(errError As ErrObject) MsgBox errError.Number & " " & errError.Description & vbCrLf & vbCrLf & "The error has been logged" End Sub
Any thoughts or comments on how to do this in vb.net would be appreciated.




Reply With Quote