tmetz - 2004-11-05

Hello,

I have written a small experimental class, called verbose_exception. The aim is to be able to know where an exception occurs through tracing the functions stack between the "throw" and the final "catch".

Best is an example :

The verbose_exception message looks like that :
---------------------------------------
Unable to read myFile.bin 2 : bad_format
while executing "int test_ex ()" (file test_verbose_exception.cpp line 42)
in "int test_ex ()" (file test_verbose_exception.cpp catch line 48)
in "void test ()" (file test_verbose_exception.cpp catch line 58)
in "int main ()" (file test_verbose_exception.cpp catch line 95)
---------------------------------------
=> first line : error message + user message
=> second line : file, line and function where the exception is thrown
=> other lines : functions stack

Are you interrested ?

to get the experimental class : http://tmetz.free.fr/verbose_exception-0.2-1.tgz

--------------------------------------------------
The following code is used to generate the example
--------------------------------------------------
// Declare specific exception
VE_DECLARE_VERBOSE_EXCEPTION(bad_format, "bad_format")

// User procedure
int test_ex()
{
    try {
        int fileId = 2;
        char * fileName = "myFile.bin";

        VE_THROW_VERBOSE_EXCEPTION(bad_format,
                                "Unable to read " << fileName <<  " "
                                 << fileId << " : " << VE_WHAT);
        return 0;
    }
    VE_CATCH_RETHROW()
}

// User procedure
void test()
{
    try {
        test_ex();
    }
    VE_CATCH_RETHROW()
}
int main()
{
    try {
        cout << " *** User exception (from user code) ***" << endl;
        test();
    }
    VE_CATCH_PRINT()
    catch (...) {
        cerr << "Undefined exception" << endl;
    }
    return 0;
}