Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you can use boost then you should do so. Refer to <a href="http://www.boost.org/doc/libs/1_39_0/libs/exception/doc/boost-exception.html" rel="nofollow noreferrer">this link</a> on how to use boost exceptions. You can also design your own exception class hierarchy as stated by other answers, but you need to take care of subtle aspects like 'nothrow' requirements from the 'what' method. A basic design on how this can be done on the lines of boost::exception is explained below:-</p> <pre><code>#include &lt;string&gt; #include &lt;memory&gt; #include &lt;stdexcept&gt; /************************************************************************/ /* The exception hierarchy is devised into 2 basic layers. System exceptions and Logic exceptions. But Logic exceptions are convertible to the ultimate base 'System' in the system layer. *************************************************************************/ // the system exception layer namespace ExH { namespace System { // This is the only way to make predefined exceptions like // std::bad_alloc, etc to appear in the right place of the hierarchy. typedef std::exception Exception; // we extend the base exception class for polymorphic throw class BaseException : public Exception { public: BaseException() throw() {} explicit BaseException(char const* /*desc*/) throw() : Exception() {} BaseException(BaseException const&amp; that) : Exception(that) {} virtual void raise() const { throw *this; } // used to throw polymorphically virtual ~BaseException() throw () {} }; // module level classes compose and catch the descriptive // versions of layer-exceptions class DescriptiveException : public BaseException { public: explicit DescriptiveException (char const* description) throw() : description_(description) { } explicit DescriptiveException (std::string const&amp; description) throw() : description_(description.c_str()) { } virtual ~DescriptiveException () throw () {} DescriptiveException (DescriptiveException const&amp; src) throw() : BaseException(src) { this-&gt;description_ = src.description_; } DescriptiveException&amp; operator= (DescriptiveException const&amp; src) throw() { if (this != &amp;src) { this-&gt;description_ = src.description_; } return *this; } /*virtual*/ char const* what () const throw() { return description_; } /*virtual*/ void raise() const // used to throw polymorphically { throw *this; } protected: DescriptiveException () throw (); private: char const* description_; }; } } // the logic exception layer namespace ExH { namespace Logic { // Logic::Exception inherits from System::Exception for the // following reason. Semantically for some part of the // system particular instance of Logic::Exception may seem as // opaque System::Exception and the only way to handle it would // be to propagate it further. In other words Logic::Exception // can be seamlessly "converted" to System::Exception if there is // no part of the system interested in handling it. // class BaseException : public System::BaseException { public: BaseException() throw() {} explicit BaseException(char const* desc) throw() : System::BaseException(desc) {} BaseException(BaseException const&amp; that) : System::BaseException(that) {} virtual void raise() const { throw *this; } // used to throw polymorphically virtual ~BaseException() throw () {} }; // module level classes compose and catch the descriptive // versions of layer-exceptions class DescriptiveException : public BaseException { public: explicit DescriptiveException (char const* description) throw() : description_(new std::string(description)) { } explicit DescriptiveException (std::string const&amp; description) throw() : description_(new std::string(description)) { } DescriptiveException(DescriptiveException const&amp; src) throw() : BaseException(src) { // copy the string std::string* str = new std::string(src.description_.get()-&gt;c_str()); description_.reset(str); } virtual ~DescriptiveException () throw () {} /*virtual*/ char const* what () const throw() { return description_-&gt;c_str(); } /*virtual*/ void raise() const { throw *this; } private: DescriptiveException&amp; operator= (DescriptiveException const&amp; src) throw(); // copy disabled std::auto_ptr&lt;std::string&gt; description_; // do not use std::string, as it can throw }; } } /************************************************************************/ /* Users of the exception hierarchy compose specific exceptions as and when needed. But they can always be caught at the System::Exception base class level. Some of the standard conversion examples are demonstrated :- class MyClass { public: class Exception_ {}; typedef Compound &lt;Exception_, Logic::DescriptiveException&gt; Exception; class InvalidArgument_ {}; typedef Compound &lt;InvalidArgument_, Exception&gt; InvalidArgument; class NotInitialized_ {}; typedef Compound &lt;NotInitialized_, Exception&gt; NotInitialized; public: void myFunction1() const throw(NotInitialized); void myFunctionN() const throw(NotInitialized); }; void MyClass::myFunction1() const { throw NotInitialized("Not Inited!"); } void MyClass::myFunctionN() const { try { // call myFunction1() } catch(NotInitialized const&amp; e){ // use e } } This has to be per-class basis. The exposed module will have an exception specification which will catch all the sub-class exceptions. The calling module will in turn rely on this exception-specification. This will allow us to have generalized exception-catching at the application-level and more specialized exception-catching at the specific module level. */ /************************************************************************/ // a simple template to compose the exceptions as per conversion requirements namespace ExH { template &lt;typename Type, typename Base&gt; class Compound : public Base { public: explicit Compound (char const* description) throw () : Base(description) {} explicit Compound (std::string const&amp; description) throw () : Base(description) {} Compound (Compound const&amp; src) throw () : Base(src) {} virtual ~Compound () throw () {} protected: Compound () throw () {} private: Compound&amp; operator= (Compound const&amp; src) throw (); // disable copy }; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload