views:

396

answers:

4

What is a good use case for uncaught_exception?

A: 

In my case building web apps, I use uncaught exception to halt execution, drop a page to the viewer letting them know than an error was encountered, and that it has been forwarded to our developers for review, and then log the error.

If one happens, then we know to check it and build a handler, so if it ever happens again, it will be caught and handled gracefully.

The main point is just to make sure the errors you HAVEN'T planned on will be handled somehow, and that you will be notified about them so they don't recur.

Eli
+1  A: 

Probably none

fizzer
+4  A: 

Herb Sutter seems to give good advice here. He doesn't know of a good use for it and says that some cases where it appears to be useful don't really work.

David Norman
+1  A: 

uncaught_exception can be used in destructors, to determine whether they are being executed in the context of an exception (where a throw will terminate the program). I don't disagree that the philosophy is slightly flawed, but it depends on your use of exceptions - if your exception is a recoverable error, it may be more convenient to just try to fix it rather than let another part of the code attempt to deal with it as you normally would.

It is also useful if you have code requiring an active exception (this is rare, but occasionally you have an exception control library that will use throw; to get the current exception, but that will cause a termination if there is none, so uncaught_exception can be used to determine whether that will abort (and if so, possibly throw an exception!). An example is the new exception facilities, which are also a part of boost.

coppro