Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I thought it might be interesting to post some real code for a change. This is the exception class my own utility library uses:</p> <pre><code>//--------------------------------------------------------------------------- // a_except.h // // alib exception handling stuff // // Copyright (C) 2008 Neil Butterworth //--------------------------------------------------------------------------- #ifndef INC_A_EXCEPT_H #define INC_A_EXCEPT_H #include "a_base.h" #include &lt;exception&gt; #include &lt;sstream&gt; namespace ALib { //------------------------------------------------------------------------ // The only exception thrown directly by alib //------------------------------------------------------------------------ class Exception : public std::exception { public: Exception( const std::string &amp; msg = "" ); Exception( const std::string &amp; msg, int line, const std::string &amp; file ); ~Exception() throw(); const char *what() const throw(); const std::string &amp; Msg() const; int Line() const; const std::string &amp; File() const; private: std::string mMsg, mFile; int mLine; }; //------------------------------------------------------------------------ // Macro to throw an alib exception with message formatting. // Remember macro is not in ALib namespace! //------------------------------------------------------------------------ #define ATHROW( msg ) \ { \ std::ostringstream os; \ os &lt;&lt; msg; \ throw ALib::Exception( os.str(), __LINE__, __FILE__ ); \ } \ } // namespace #endif </code></pre> <p>And this is the .cpp file:</p> <pre><code>//--------------------------------------------------------------------------- // a_except.h // // alib exception handling stuff // // Copyright (C) 2008 Neil Butterworth //--------------------------------------------------------------------------- #include "a_except.h" using std::string; namespace ALib { //--------------------------------------------------------------------------- // exception with optional message, filename &amp; line number //------------------------------------------------------------------------ Exception :: Exception( const string &amp; msg ) : mMsg( msg ), mFile( "" ), mLine(0) { } Exception :: Exception( const string &amp; msg, int line, const string &amp; file ) : mMsg( msg ), mFile( file ), mLine( line ) { } //--------------------------------------------------------------------------- // Do nothing //--------------------------------------------------------------------------- Exception :: ~Exception() throw() { } //------------------------------------------------------------------------ // message as C string via standard what() function //------------------------------------------------------------------------ const char * Exception :: what() const throw() { return mMsg.c_str(); } //------------------------------------------------------------------------ // as above, but as C++ string //------------------------------------------------------------------------ const string &amp; Exception :: Msg() const { return mMsg; } //--------------------------------------------------------------------------- // File name &amp; line number //--------------------------------------------------------------------------- int Exception :: Line() const { return mLine; } const string &amp; Exception :: File() const { return mFile; } } // namespace // end </code></pre>
 

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