Advertisement

Exception class

Started by February 21, 2000 07:17 AM
0 comments, last by null_pointer 24 years, 7 months ago
I've been working on making some classes, and I'm trying to add an exception class (or perhaps several classes) to my code to handle exceptions. It would make debugging much easier! The way I see it, there's three different ways of doing this. #1 -- THE WAY OF THE BLOATED GLOBAL I could make one global exception class, and have it be able to hold data to describe all of the different types of exceptions. (yuck!) #2 -- A CLASS FOR EVERYTHING, AND EVERYTHING IN ITS CLASS I could make a generic class which describes the exceptions, then derive any specific exception types. Let the compiler do the type-checking for the catch() blocks. Like this: class Exception { virtual void Display(); // etc. }; class FileException : public Exception { virtual void Display(); // override // etc. }; class DialogException : public Exception { virtual void Display(); // override // etc. }; #3 -- USE SCOPE TO MAKE YOUR MOUTH FEEL FRESH This is the idea I've been playing around with. Each class would have its own Exception class "embedded" (as it were) in the class declaration. Declare it like this: class File { public: class Exception { public: Exception(); virtual ~Exception(); public: void Display(); }; public: File(); virtual ~File(); // etc. }; As per the example, each class would have its own Exception type. You could check the type of the exception like this: try { // some code here } catch( File::Exception ) { // file-specific exception handling code here } catch( Dialog::Exception ) { // dialog-specific exception handling code here } catch( ::Exception ) { // catch generic (or user-defined) exceptions } It looks nice () to me, but the problem with this is that I can't define a global Exception class as an interface without using something other than the name Exception. I'd probably have to use StdException or something like that. I thought scope resolution was supposed to resolve difficulties like this. Or am I just going about it wrong? If anybody knows how to do it this way, please let me know. I think that it's logical. Anyway, I'm relying on the theory that the class should not trust the programmer who uses it to do the proper debugging. It should be as fail-safe as possible (within the constraints of good coding). Also, classes should be as independent as possible; otherwise you'd have to drag in the whole library whenever you wanted to use just one class . And the classes won't be very independent when they rely on global Exception-derived classes. Which of those three methods do you think is best? - null_pointer Edited by - null_pointer on 2/21/00 2:29:12 PM
IMHO ....

Forget globals, it's soo damn ugly !!

I think the #2 is the ideal solution for an OOP aproach.

Think about it ...

The exception class is polymorphic, so you don't have to worry with the kind of exception generated.

If you don't have to worry with the kind of exception generated, you could use only one catch block, which will make your code more readeable and smaller.



Edited by - Strauss on 2/22/00 12:41:40 PM

Edited by - Strauss on 2/22/00 12:42:32 PM

This topic is closed to new replies.

Advertisement