Debug exceptions

S

seenutn

Hi,
These questions are on C++ and Exceptions.

1) How can I debug through gdb to find the location where exception
occured (Putting a breakpoint in exception handler will not help me as
the stack has already woundback and I have lost the information about
the code where it occured).

2) If I do "catch (...)", it will catch all exceptions. But in my
exception handler I want to know the type of exception that has
occured.

Note: I am using linux, gcc 3.4.3, gdb 6.1

Regards,
Seenu.
 
J

jesper

Hi,
These questions are on C++ and Exceptions.

1) How can I debug through gdb to find the location where exception
occured (Putting a breakpoint in exception handler will not help me as
the stack has already woundback and I have lost the information about
the code where it occured).

2) If I do "catch (...)", it will catch all exceptions. But in my
exception handler I want to know the type of exception that has
occured.

Note: I am using linux, gcc 3.4.3, gdb 6.1

Regards,
Seenu.


catch(Type &exception)
{
}
I think.. Maybe I didn't understand your question
/Jesper
 
S

seenutn

If I know the Type of the exception, then I can use the above syntax.
But if I am using
catch (...), I am not able to make out the exception.

For eg: If I know the exception, I can use "catch Type& e" and within
catch I can do cout of "e.what ()", but what if it is "catch (...)"?


Regards,
Seenu.
 
D

Daniel T.

Hi,
These questions are on C++ and Exceptions.

1) How can I debug through gdb to find the location where exception
occured (Putting a breakpoint in exception handler will not help me as
the stack has already woundback and I have lost the information about
the code where it occured).

2) If I do "catch (...)", it will catch all exceptions. But in my
exception handler I want to know the type of exception that has
occured.

Note: I am using linux, gcc 3.4.3, gdb 6.1

Best practice is to have all exception classes inherit from
std::exception, that way you can do "catch (const exception& e)" and
then query "e.what()" and hopefully that will give you a clue where it
came from.
 
D

Daniel T.

If I know the Type of the exception, then I can use the above syntax.
But if I am using
catch (...), I am not able to make out the exception.

For eg: If I know the exception, I can use "catch Type& e" and within
catch I can do cout of "e.what ()", but what if it is "catch (...)"?

At the end of main, I often have something like this:

catch ( const exception& e ) {
// send e.what() to some error console
}
catch ( ... ) {
// send "unknown exception: who needs to die" to the error console
}

You see, if properly written, that catch(...) should never get used.
 
I

Ivan Vecerina

: If I know the Type of the exception, then I can use the above syntax.
: But if I am using
: catch (...), I am not able to make out the exception.
:
: For eg: If I know the exception, I can use "catch Type& e" and within
: catch I can do cout of "e.what ()", but what if it is "catch (...)"?

It is a recommended practice to derive all your custom exception
classes from std::exception, or a relevant subclass of it
(such as std::runtime_error).

The 'most generic' catch handling should then be:
catch( std::exception& x )
You can then access x.what() for a description of the error,
but also typeid(x).name() for a string that describes its
type (look up RTTI or typeid in your usual C++ reference).

Cheers,
Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top