OLE Server error handling

by Peter Wone - GUI Computing

Errors occurring in OLE server objects a royal pain in VB4. Typically they take the form "Error -12323524895245 Somewhere, something went wrong" which is less than useful to one trying to work out the problem.

Once you really get into OLE as a technique, you frequently have not only a lot of classes floating around, you also have an indeterminate number of instances of those classes due to user interaction. For example, you might let the user create new MDI child windows.

Because it was driving me barmy, I solved the problem. First I decided that the rather ambiguous name property (name of what?) should be the Class property, and then I decided that all objects need an Instance property.

So I implemented them. The Class property is read-only and derives itself from the class.Name, so it's completely automatic. The class.Instance property is basically a tag for the object. You have to set it yourself after you instantiate the object. If you don't, the object initialisation event sets it to "<instance name not set>."

I then elected to include my standard stack class module VariantStack so that I could create an error context stack.

Then I created an extended Err object, called ErrEx, which has a method called ErrEx.Raise. This concatenates the object class name, a tab character and the object instance name and pushes this string onto the error stack, before raising an error. It's intended for OLE servers, so it automatically adds vbObjectError to the error code.

When an error occurs, the error handler which detected it can either take action or report it to the user. In the latter case, instead of the classical MsgBox, it should invoke the method ErrEx.MsgBox, which requires no parameters because it knows all about the error stack.

It reports the error number and error description most recently reported. Clicking the details button reveals a list box containing the chain of object invocations leading to the error condition.

The sample project includes a form object with an Action property. The Sub Main routine creates an instance of the form object and assigns an invalid value to the object instance's Action property. The object instance responds by invoking the ErrEx.Raise method.

The Sub Main error traps does not have a specific handler for the raised error so it invokes ErrEx.MsgBox, resulting in an error dialog as described above.

Written by: Peter Wone
Feb 1996