stack traces with any exception class

Define THROW_EXCEPTION(E) which takes an exception class instance,
and uses it to construct an instance of a class which is a subclass
of E and ExceptionMixin.  The original instance is discarded, and
the newly constructed sub-class is thrown.  Equivalent to
"throw E;".

Define THROW_EXCEPTION2(ETYPE,MSG) which takes an exception class
type, and argument.  Directly constructs a ExceptionMixin sub-class
with the given message argument.  Equivalent to
"throw ETYPE(MSG);".

Define PRINT_EXCEPTION2(E, FP) If E is a instance of a sub-class of
ExceptionMixin then write information to FP (FILE*).

Define SHOW_EXCEPTION(E) If E is a instance of a sub-class of
ExceptionMixin then return a std::string with information.
This commit is contained in:
Michael Davidsaver
2011-03-02 16:31:08 -05:00
parent 9fd158df1f
commit 0c61ac0833
4 changed files with 280 additions and 161 deletions

View File

@@ -63,6 +63,22 @@ void testBaseException(FILE *fp) {
fprintf(fp,"PASSED\n");
}
void testLogicException(FILE *fp) {
try {
THROW_EXCEPTION(std::logic_error("There is a logic_error"));
} catch (std::logic_error& be) {
fprintf(fp,"\n\n%s\n\n", be.what());
PRINT_EXCEPTION2(be, fp);
}
try {
THROW_EXCEPTION2(std::logic_error, "There is another logic_error");
} catch (std::logic_error& be) {
fprintf(fp,"\n\n%s\n\n", be.what());
fprintf(fp,"%s\n", SHOW_EXCEPTION(be).c_str());
}
}
int main(int argc,char *argv[])
{
FILE *fp=NULL;
@@ -71,6 +87,7 @@ int main(int argc,char *argv[])
if(!fp) fprintf(stderr,"Failed to open test output file\n");
}
if(!fp) fp=stdout;
testLogicException(fp);
testBaseException(fp);
return(0);
}