C++ Exceptions Derived From Stl::exception ?

free web hosting
Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

C++ Exceptions Derived From Stl::exception ?

qwijibow
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

CODE

class interpreter_exception : public stl::exception {};



work fine ???

thanks.

Reply

miCRoSCoPiC^eaRthLinG
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++)

CODE

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 exception
try
{

   // 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.

 

 

 


Reply

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

example..

CODE

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 ???

Reply

miCRoSCoPiC^eaRthLinG
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 tongue.gif

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 wink.gif

Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. creating exception class c - 6.85 hr back. (1)
  2. c built in exceptions - 9.10 hr back. (1)
  3. creating custom exceptions in .net 2005 c - 9.68 hr back. (1)
  4. stl exception - 0.37 hr back. (3)
  5. how to make my own exception class in c - 21.18 hr back. (1)
  6. deriving from exception c - 25.85 hr back. (1)
  7. derive from exception c - 29.50 hr back. (1)
  8. derive your exceptions from stl - 34.58 hr back. (1)
  9. why should you derive your exceptions from stl - 34.63 hr back. (1)
  10. stl exceptions - 1.59 hr back. (5)
  11. catch von stl error - 44.48 hr back. (1)
  12. dérivée exception c - 45.85 hr back. (1)
  13. implementing own c exception - 52.42 hr back. (1)
  14. exception class derive c - 63.30 hr back. (1)
Similar Topics

Keywords : c, exceptions, derived, stl, exception

  1. Help Needed - Problem In Exception Handling
    (4)


      Looking for c, exceptions, derived, stl, exception






*SIMILAR VIDEOS*
Searching Video's for c, exceptions, derived, stl, exception
advertisement




C++ Exceptions Derived From Stl::exception ?



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE