Jump to content
xisto Community
Sign in to follow this  
qwijibow

C++ Exceptions Derived From Stl::exception ?

Recommended Posts

For my c++ course work, i need to complete a partially written c++ porgram.

this program throws an exception class called "interpreter_exception".
i need to write this exception class.

ive noticed that in the catch blocks, this exception is treated very similar, if not exactly the same as the STL exception class, so im thinking it would be a good idea to make interpreter_exception a derived class, from super class stl::exception.

what do i need to do in order to make this work ? are there any virtual / pure virtual functions i will ned to define ? or will a simple

class interpreter_exception : public stl::exception {};


work fine ???

thanks.

Share this post


Link to post
Share on other sites

Yup - that's the way to go. To define your own type of exceptions to handle custom exception events, you should always derive from the stl::exception. But since you're inheriting the class, and not implementing any kind of interface, you don't really need to redefine any of the method bodies in superclass exception.

What you should include though, is a default message, that can be printed out of this class + the ability to pass and store a custom error message to this class.

Let me give you an example ( I might not be correct with the syntax - am coverting an idea I got from one of my Java courses into C++)

using namespace std;...........class interpreter_exception : public stl::exception {private:    // Var to hold error message    string message;public:    // Constructor    interpreter_exception ( string m )     {         // If no message was passed to the constructor, use the default message       if ( m.length == 0 )       {           message = "Interpreter Exception Detected.";       }       else       {            message = m;       } // end if-else    };  // end constructor    // Method to print out the error message        public void Message ()     {        cout << message;    }; // end Message}; // end interpreter_exception class// This is main program body where you throw the exceptiontry {    // Do Something (In Java you can use the "new" keyword to call the constructor    // and pass a custom message to the exception class. I'm kinda confused,    // in C++ you probably don't need to use the "new" - or maybe optionally used.    throw new interpreter_exception ( "Some error occured" );    // OR    throw interpreter_exception; }  catch( interpreter_exception e ){       // Print out the error message    e.Message();} // end try-catch


Hope this will guide you to creating custom exception handlers in future... the concept is extremely easy, although I'd be able to illustrate it better with Java.

P.S. What you've shown, would do too - except that it won't show the message "interpreter exception detected" or whatever message you pass it to. It will try print out some standard error message that the exception class has.

Share this post


Link to post
Share on other sites

i thought the stl exception class had that built in ???

example..

throw my_exception("divide by zero !!");catch (my_exception Ex) { cout << Ex.what() << endl; }

where the constructor, and what() finction are built into stl exception ???

Share this post


Link to post
Share on other sites

Yup - you're right - I wasn't clear enough in my explanation - what I demonstrated was the exact same thing - but designed by yourself - I mean when you inherit from stl:exception, the sub-class becomes a kind of exception too - and thus it is throwable. As for the message part - you can use the stl's what() method or define your own - as I showed in the code. what() is probably limited to text messages, whereas, this way if you define your own error message system - you can have a lot more fun spitting out funky messages :rolleyes: Nothing else needed - as in no other functions to override or implement. Just these are enough to get you going. P.S. - I didn't know about the what() class either - lol.. so I provided my own way here.. My C++ OOP syntax isn't that strong - it's way better in Java.. But gimme something to code in procedural C/C++ and I'll rock :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.