Discussion Locked

Error tracking in vb classes

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I'm a little bit confused about how to track the errors in a class module.

Do i do it the regular was on error goto my error handler then handle the thing and get out of my class.

Problem is the procedure/object that called the class should beaware of it. Do i program my class use functions to return a status. Do I add a property to my class that is the status or do I do an error raise before exiting my class in order to be sure that my calling object is aware of the error.

I don't know about you, but I personally do not really like the err.raise solution. There are already errors enough in the world. It's not my job to create more ;)

Can you please tell me what's your opinion about it or send your solutions.

It has caused already numerous discussions within our development team.

3 replies

by albert franco ii

First off, I must tell you I am not a VB programmer. I far and away prefer Delphi, among other things for its solid error handling.

Now on to the theory of error handling and object oriented programming. Your class should be fully self-contained and communicate EVERYTHING that the rest of the program needs to know about it. This is done by using properties and events. err.raise is just such an event and is THE professional way to alert the rest of the application that your class has had a problem it can not fix.

In general any operation that can produce an error which is not recoverable in the same operation should return either a boolean value to its caller, push an error message onto a private stack, or call the err.raise method.
The first option is a good choice for just about any procedure within a class. This allows the class to quietly handle its own problems.

The second choice is used when the class must communicate to its caller. The private error stack can be a property of the class (status flag as you've suggested or a string list with various problems or messages).

The third choice is for "BIG" errors that may need to be handled by the application or form level. These are usually errors that can bomb the aplication or affect the stability of the system.

As you can see each approach is appropriate in certain situations. And don't worry about raising errors...the whole idea of raising errors is to be able to properly handle them. If you don't raise them they don't get handled and the user gets pissed because your program behaves badly.

Good Luck,
Al Franco, II
APF Creations
Pamplona, Spain

This discussion is closed to new replies.