<<< /en/programming

Antti-Juhani Kaijanaho: C++ exceptions revisited

I wrote two days ago:

Is there a mechanism to figure out what exception it is that fails to be caught by any exception handler on the stack? Of course, it can be caught by using catch (...), but that just tells me as much as G++'s default handler: that some exception was thrown.

Matus Telgarsky later pointed out to me in private mail that G++ does have a better terminate() handler, used by default in G++ 3.4. I could also invoke it in older G++ by adding
std::set_terminate( __gnu_cxx::__verbose_terminate_handler );
which I did. Based on the information it gives I eventually tracked the problem to my use of safe_ptr<AbSyn::AST>::release in my safe_ptr-downcasting functions in ast.hh. I had written that code originally using std::auto_ptr but due to problems I then implemented a reference-counted replacement for it. I had tried to make safe_ptr's interface to look as similar to std::auto_ptr's interface as possible, which now turned out to be a mistake. It turned out that there were four active aliases for things I passed to release at the time, which quite appropriately triggered an exception. However, unfortunately, I had given the downcasting functions exception specifications not listing that particular exception. Now, the semantics of an exception not listed being thrown in such a situation is that the program is immediately terminated.

This problem highlights a C++ exception misfeature: it allows calling a function without an exception specification (which essentially means that it might throw anything) from a function with an exception specification (where throwing an exception not listed invokes immediate termination) without so much as a warning to guide the programmer!

2004-05-15T16:33+0300 - /en/programming


Trackback url: http://antti-juhani.kaijanaho.info/blog/en/programming/cc-exceptions-2.trackback (trackback on rikki / trackback is broken)

Your Comment