views:

175

answers:

5

Hi all,

I am programming in C++ on Linux platform.

My program terminates with this (unhandled???) exception:

"terminate called after throwing an instance of 'long'" Aborted

The code that is throwing the exception is inside try-catch block, then why should this happen?? The exception is thrown while returning from a function.

I am used to C programming and have very little experience in C++ (which is the main problem). I don't know how to debug this issue. I don't expect a solution but a direction/pointer for debugging this problem.

Thanks in advance.

A: 

you are probably catching the wrong exception type

use

catch(long)

or

catch(...)
catwalk
I am using catch(...), and still the expection....
puffadder
+5  A: 

It there anywhere on the call-stack with a exception specification or here? If there is then you might have this problem - you probably want to remove all of them.

If you are using gcc, then you can add this code first thing in main():

#ifdef __GNUC__
    std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
#endif // ifdef __GNUC__

(More details at http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt02ch06s02.html)
Which will give you a better traceback from such exceptions.

Douglas Leeder
where should I add this code????
puffadder
@puffadder: in main.
jon hanson
A: 

Normally, I would recommend to set a breakpoint in the constructor of the thrown type -- but in this case ... I must admit to never have experienced that somebody has thrown a long like

throw 42;

That seems to me strange. Some debuggers might be able to catch an exception when it is thrown.

Is the called function yours?

Juergen
throw 42 is cool - although it should not survive a code review. But still - it's cool.
Tobias Langner
That's the reason, the biggest computer ever build needs 7.5 million years to calculate it ;-) just because some ignorant code reviewers eliminated the line ...
Juergen
42 isn't a long, it's an int. :)
unwind
right. it should be "throw (long)42;" of course.
Juergen
Or `throw 42L;`
GMan
Right, I did not program in C++ for a long time ;-)
Juergen
+7  A: 

You can run your application under gdb (having built it with debug info using -g) and get it to break when an exception is thrown using the command:

(gdb) catch throw

This will take you to the origin of the exception. Some more info is available in this question:

Note that it is somewhat unusual to throw an ordinal type (such as a long). It may be in some temporary code, so grepping around might find it quickly enough.

gavinb
A: 

Use set_terminate to break GDB

Example for set_terminate() is here

When it trigged - use bt command in GDB to see backtrace