Compare commits
4 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 97b60f5cb9 | |||
| c78ce64273 | |||
| 2f9d1b7a1f | |||
| 269539741c |
3 changed files with 23 additions and 16 deletions
|
|
@ -19,7 +19,7 @@ Example
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
||||||
#include "exceptions.h"
|
#include <exceptions/exceptions.h>
|
||||||
|
|
||||||
// Use this macro to build up your hierarchy of exceptions
|
// Use this macro to build up your hierarchy of exceptions
|
||||||
EXCEPTION( Exception, Existential_Observation );
|
EXCEPTION( Exception, Existential_Observation );
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "exceptions.h"
|
#include "exceptions/exceptions.h"
|
||||||
|
|
||||||
// Use this macro to build up your hierarchy of exceptions
|
// Use this macro to build up your hierarchy of exceptions
|
||||||
EXCEPTION( Exception, Existential_Observation );
|
EXCEPTION( Exception, Existential_Observation );
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#ifndef __EXCEPTIONS_H__
|
#pragma once
|
||||||
#define __EXCEPTIONS_H__
|
#ifndef EXCEPTIONS_H
|
||||||
|
#define EXCEPTIONS_H
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
@ -19,7 +20,7 @@
|
||||||
EXCEPTION(Exception,Buddhist_Observation);
|
EXCEPTION(Exception,Buddhist_Observation);
|
||||||
creates an new exception class "Buddhist_Observation", derived from the base "Exception"
|
creates an new exception class "Buddhist_Observation", derived from the base "Exception"
|
||||||
*/
|
*/
|
||||||
#define EXCEPTION(Super,Current) class Current : public Super {public: Current ( const std::string & desc, const std::string & func="?", const std::string & f="?", const int l=-1 ) : Super (desc,func,f,l) {name = #Current;} }
|
#define EXCEPTION(Super,Current) class Current : public Super {public: Current ( const std::string & desc, const std::string & func="?", const std::string & f="?", const int l=-1, const std::string & n=#Current ) : Super (desc,func,f,l,n) {} }
|
||||||
|
|
||||||
|
|
||||||
/** A shortcut to throw an exception without having to type ",E_INFOS"
|
/** A shortcut to throw an exception without having to type ",E_INFOS"
|
||||||
|
|
@ -35,8 +36,6 @@
|
||||||
class Exception : public std::exception
|
class Exception : public std::exception
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
//! Name of the current exception class
|
|
||||||
std::string name;
|
|
||||||
|
|
||||||
//! Description of the exception
|
//! Description of the exception
|
||||||
std::string description;
|
std::string description;
|
||||||
|
|
@ -50,6 +49,12 @@ protected:
|
||||||
//! Line where the exception has been raised
|
//! Line where the exception has been raised
|
||||||
int line;
|
int line;
|
||||||
|
|
||||||
|
//! Name of the current exception class
|
||||||
|
std::string name;
|
||||||
|
|
||||||
|
//! Assembled message
|
||||||
|
std::string message;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
//! Constructor of the exception
|
//! Constructor of the exception
|
||||||
/*!
|
/*!
|
||||||
|
|
@ -59,21 +64,23 @@ public:
|
||||||
Use the E_INFOS macro to raise the exception, for example :
|
Use the E_INFOS macro to raise the exception, for example :
|
||||||
throw( Exception( "Shit evolves", E_INFOS );
|
throw( Exception( "Shit evolves", E_INFOS );
|
||||||
*/
|
*/
|
||||||
Exception( const std::string & desc, const std::string & func, const std::string & f, const int l )
|
Exception( const std::string & desc, const std::string & func, const std::string & f, const int l, const std::string & n = "Exception" )
|
||||||
: description(desc), function(func), file(f), line(l) {}
|
: description(desc), function(func), file(f), line(l), name(n)
|
||||||
|
|
||||||
//! The destructor is not allowed to throw exceptions
|
|
||||||
virtual ~Exception() throw () {}
|
|
||||||
|
|
||||||
//! The method to use for printing the complete description of the exception
|
|
||||||
std::string what()
|
|
||||||
{
|
{
|
||||||
std::ostringstream msg;
|
std::ostringstream msg;
|
||||||
msg << description << " (<" << name << "> in " << function << " at " << file << ":" << line << ")";
|
msg << "<" << name << "> " << description << "\t\t" << function << " @ " << file << ":" << line << "";
|
||||||
|
message = msg.str();
|
||||||
|
}
|
||||||
|
|
||||||
return msg.str();
|
//! The destructor is not allowed to throw exceptions
|
||||||
|
virtual ~Exception() noexcept {}
|
||||||
|
|
||||||
|
//! The method to use for printing the complete description of the exception
|
||||||
|
const char* what() const noexcept
|
||||||
|
{
|
||||||
|
return message.c_str();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __EXCEPTIONS_H__
|
#endif // EXCEPTIONS_H
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue