Visual Basic Version: VB 4.0, VB 5.0, VB 6.0
Issue: If I have code in the Class_Initialize event and that code generates an error, what will happen?
Errors in the Initialize event for a class will be raised to the line of code that created the object. If you use the following syntax:
Private m_myObject as CMyClass
Set m_myObject = New CMyClass
then the Set statement will create the object. You can add error handling in the procedure containing this statement to trap initialization errors.
If you use the following syntax:
Private m_myObject as New CMyClass
then the first time you use the object, VB will create the object. In this case you need error handling in any procedure that uses the object to ensure the initialization errors are trapped.
Visual Basic Version: VB 4.0, VB 5.0, VB 6.0
Issue: I read in the VB documentation that code should always handle errors in the Class_Terminate event procedure. Why is this so important?
An error generated in the Terminate event for a class is not raised to the code that terminated the object. Therefore the error will be fatal to the application. This also means that you cannot use Err.Raise in the Terminate event because it will not raise the error to the application but will generate an unhandled error.
The recommended technique for error handling in the Terminate event is to use On Error Resume Next. This ensures that no untrapped error is generated.
Visual Basic Version: VB 5.0, VB 6.0
Issue: When the application generates an unexpected error, I would like to log it to the NT event log. How do I do it?
VB 5.0 has a new method on the App object that allows you to log information to the NT event log. For example:
App.LogEvent sPrintMsg, lLogType
This will add the string defined in sPrintMsg to the NT event log. lLogType is the type of log entry.
Visual Basic Version: VB 5.0, VB 6.0
Issue: I added the code to log to the NT event log. Why doesn't it work?
To prevent you from filling up your NT event log during design time, the VB team decided it would be good to disable the logging at design time. In order to test your NT event log code, you need to compile your application first and then run your test.
Visual Basic Version: VB 5.0, VB 6.0
Issue: What type of thing would I put into an error class?
One standard method we put into our error class is the ErrorLog method. This encapsulates the code to build a string and log it into the NT event log. For example:
Public Sub ErrorLog(sMessage As String, lLogType As Long) Dim sPrintMsg As String
sPrintMsg = Now & Space(5) & sMessage
If lLogType = vbLogEventTypeError Then sPrintMsg = sMessage & vbCrLf & _ " Error: [" & Err.Number & "]: " & _ Err.Description & " Error in: " & Err.Source End If
' Write it in the event log App.LogEvent sPrintMsg, lLogType
' Output the string to the debug window as well Debug.Print sPrintMsg
End Sub
Notice how this code logs to the NT event log. Since the logging does not work at design-time, it prints to the Immediate window at design-time.
| All contents © 2004 InStep Technologies, Inc. All rights reserved. |