oz10
oz10

Reputation: 158304

When would I use uncaught_exception?

What is a good use case for uncaught_exception?

Upvotes: 6

Views: 1084

Answers (3)

coppro
coppro

Reputation: 14516

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.

Upvotes: 2

David Norman
David Norman

Reputation: 19879

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.

Upvotes: 7

fizzer
fizzer

Reputation: 13796

Probably none

Upvotes: 2

Related Questions