If I have a method marked throw(), e.g.
void method() throw()
{
// do some stuff, call other functions
}
and yet exception does happen inside, gcc will terminate the application (with message “terminate called after throwing an instance of ‘xyz'”).
Is there a way to avoid this behaviour?
For example, a command-line switch to ignore throw() stuff or force eh_frame generation. Etc.
Did you try the GCC manual?
It doesn’t force generate of EH frames, but it should stop the call to
std::unexpected()and so might be useful for your case.As the docs say, “the compiler still optimizes based on the specifications” so e.g. it doesn’t help when the call to
method()can be inlined into the catch site, because the compiler assumes the catch is not needed, because the empty exception spec says no exception will be throw, and so if an exception is thrown it doesn’t get caught. If the call tomethod()is not inlinable into the catch site it seems to work, the exception leavesmethod()without callingstd::unexpected()and can be caught higher up the stack.Edit: This will still call
std::terminate()even with-fno-enforce-eh-specs:The compiler can see that the call to
func2only calls a no-throw function, so thecatchwill never be needed and so is optimised away. When the exception is thrown, it isn’t caught.This does work with
-fno-enforce-eh-specsand doesn’t terminate:Here, when compiling
main.cc, the compiler can’t tell whetherfunc2is going to throw or not because it has no exception specification and its definition is not visible inmain.cc, so thecatchcannot be omitted. When the exception is thrown it will be caught.