#include <AlcapExcept.h>
Public Member Functions | |
Base () | |
virtual | ~Base () throw () |
const char * | what (void) const throw () |
const char * | bt (void) const throw () |
void | AppendWhat (const char *child) |
void | AppendWhat (const char *child, const char *message) |
void | AppendWhat (const char *child, const char *file, int line) |
Static Public Attributes | |
static unsigned int | gBacktraceSymbols = 5 |
The number of backtrace symbols to add to the "what" string. | |
Private Attributes | |
char | fTrace [2048] |
The backtrace when the exception was thrown;. | |
char | fWhat [256] |
What exception generated this object. |
The root exception for all exceptions explicitly thrown by the AlCap offline analysis code. All exceptions declared by this library should be derived from this class, and not std::exception. New exceptions can be derived from Except::Base by creating a new class
namespace Exception { class Child; }; class Exception::Child : public Except::Base { public: Child() {AppendWhat("Child");}; };
But the prefered way to create derived exception classes is to use the MAKE_EXCEPTION macro. This is used as follows
MAKE_EXCEPTION(Child,Base); MAKE_EXCEPTION(GrandChild,Child); ... try { throw Except::Child(); } catch (Except::Base& ex) { std::cout << ex.what << std::endl; }
This can add a backtrace at the point of the exception. The number of backtrace frames to be shown is controlled with the Except::Base::gBacktraceSymbols static variable.
In addition ther are two variant ways to construct an exception
// Throw an exception with a debugging message try { throw Except::Child("your message here"); } // Throw an exception with the source code location try { throw Except::Child(LOCATION) }
LOCATION is an auxillary macro that evaluates as `__FILE__ , __LINE__` this form is not usually needed, as it is usually obvious from the backtrace
Definition at line 63 of file AlcapExcept.h.
Except::Base::Base | ( | ) |
Definition at line 15 of file AlcapExcept.cpp.
References fTrace, fWhat, and gBacktraceSymbols.
00015 { 00016 fTrace[0] = 0; 00017 fWhat[0] = 0; 00018 #ifdef _GNU_SOURCE 00019 if (gBacktraceSymbols>0) { 00020 const unsigned int bufferSize=100; 00021 if (gBacktraceSymbols>bufferSize) gBacktraceSymbols = bufferSize; 00022 void *buffer[bufferSize]; 00023 unsigned int frames = bufferSize; 00024 if (gBacktraceSymbols<frames) frames = gBacktraceSymbols; 00025 int nFrames = backtrace(buffer,frames); 00026 char **symbols = backtrace_symbols(buffer,nFrames); 00027 if (symbols != NULL) { 00028 unsigned int len = 0; 00029 std::strcat(fTrace,"Backtrace:\n"); 00030 for (int i = 0; i<nFrames; ++i) { 00031 std::string sym(symbols[i]); 00032 std::string lib; 00033 std::string::size_type s = sym.find_last_of('/'); 00034 if (s != std::string::npos) sym = sym.substr(s+1); 00035 s = sym.find_first_of('('); 00036 if (s != std::string::npos) lib = sym.substr(0,s); 00037 sym = sym.substr(s+1); 00038 std::string::size_type t = sym.find_first_of(')'); 00039 if (s != std::string::npos 00040 && t != std::string::npos) sym = sym.substr(0,t); 00041 else sym=""; 00042 if (lib.empty() && sym.empty()) continue; 00043 len += lib.length(); 00044 len += sym.length() + 6; 00045 if (2*len>sizeof(fTrace)) break; 00046 std::strcat(fTrace," --> "); 00047 if (!lib.empty()) std::strcat(fTrace,lib.c_str()); 00048 if (!sym.empty()) { 00049 std::strcat(fTrace,": "); 00050 std::strcat(fTrace,sym.c_str()); 00051 } 00052 std::strcat(fTrace,"\n"); 00053 } 00054 std::free(symbols); 00055 } 00056 } 00057 #endif 00058 std::strcat(fWhat,"Except::Base"); 00059 }
virtual Except::Base::~Base | ( | ) | throw () [inline, virtual] |
Definition at line 74 of file AlcapExcept.h.
void Except::Base::AppendWhat | ( | const char * | child, | |
const char * | file, | |||
int | line | |||
) |
Used in conjunction with the LOCATION macro, see documentation of Except::Base
Definition at line 81 of file AlcapExcept.cpp.
References AppendWhat(), and fWhat.
00081 { 00082 AppendWhat (child); 00083 unsigned int wLen = std::strlen(fWhat); 00084 unsigned int fLen = std::strlen(file); 00085 if (fLen+wLen < sizeof(fWhat)-16) { 00086 char buf[16]; 00087 buf[0]=0; 00088 snprintf(buf,16,":%d)", line); 00089 std::strcat(fWhat," ("); 00090 std::strcat(fWhat,file); 00091 std::strcat(fWhat,buf); 00092 } 00093 }
void Except::Base::AppendWhat | ( | const char * | child, | |
const char * | message | |||
) |
Used in constructors of classes which inherit from Except::Base to add message text to the What() string.
Definition at line 70 of file AlcapExcept.cpp.
References AppendWhat(), and fWhat.
void Except::Base::AppendWhat | ( | const char * | child | ) |
Used in constructors of classes which inherit from Except::Base to add text to the What() string.
Definition at line 61 of file AlcapExcept.cpp.
References fWhat.
Referenced by AppendWhat().
const char* Except::Base::bt | ( | void | ) | const throw () [inline] |
Inherited from exception to return the name of the exception as a null terminated string.
Definition at line 82 of file AlcapExcept.h.
References fTrace.
Referenced by LoopSequence::Run().
00082 {return fTrace;}
const char* Except::Base::what | ( | void | ) | const throw () [inline] |
Inherited from exception to return the name of the exception as a null terminated string.
Definition at line 78 of file AlcapExcept.h.
References fWhat.
Referenced by LoopSequence::Run().
00078 {return fWhat;}
char Except::Base::fTrace[2048] [private] |
The backtrace when the exception was thrown;.
Definition at line 65 of file AlcapExcept.h.
char Except::Base::fWhat[256] [private] |
What exception generated this object.
Definition at line 67 of file AlcapExcept.h.
Referenced by AppendWhat(), Base(), and what().
unsigned int Except::Base::gBacktraceSymbols = 5 [static] |
The number of backtrace symbols to add to the "what" string.
Definition at line 71 of file AlcapExcept.h.
Referenced by Base().