I thought it might be interesting to post some real code for a change. This is the exception class my own utility library uses:
//---------------------------------------------------------------------------
// 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 <exception>
#include <sstream>
namespace ALib {
//------------------------------------------------------------------------
// The only exception thrown directly by alib
//------------------------------------------------------------------------
class Exception : public std::exception {
public:
Exception( const std::string & msg = "" );
Exception( const std::string & msg, int line,
const std::string & file );
~Exception() throw();
const char *what() const throw();
const std::string & Msg() const;
int Line() const;
const std::string & 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 << msg; \
throw ALib::Exception( os.str(), __LINE__, __FILE__ ); \
} \
} // namespace
#endif
And this is the .cpp file:
//---------------------------------------------------------------------------
// 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 & line number
//------------------------------------------------------------------------
Exception :: Exception( const string & msg )
: mMsg( msg ), mFile( "" ), mLine(0) {
}
Exception :: Exception( const string & msg, int line, const string & 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 & Exception :: Msg() const {
return mMsg;
}
//---------------------------------------------------------------------------
// File name & line number
//---------------------------------------------------------------------------
int Exception :: Line() const {
return mLine;
}
const string & Exception :: File() const {
return mFile;
}
} // namespace
// end