3

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.

puffadder
  • 1,814
  • 3
  • 20
  • 32
  • Since you mention in a comment that you are using `catch(...)`, which should work, could you show us some code? – Thomas Nov 24 '09 at 10:33
  • 1
    if you edit your question and add the code that exhibits this behaviour and we'll be more able to help you. – Glen Nov 24 '09 at 10:38

5 Answers5

10

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.

Community
  • 1
  • 1
gavinb
  • 19,278
  • 3
  • 45
  • 60
5

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
  • 52,368
  • 9
  • 94
  • 137
1

you are probably catching the wrong exception type

use

catch(long)

or

catch(...)
catwalk
  • 6,340
  • 25
  • 16
1

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
  • 12,378
  • 7
  • 39
  • 55
1

Use set_terminate to break GDB

Example for set_terminate() is here

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

user47695
  • 21
  • 1