Fascinating, detailed article by Herb Sutter about exception safety, object allocation, and function calls: GotW #102: Exception-Safe Function Calls.
With some humor, too!
This code attempts to “throw unique_ptr at the problem.” Many people believe that a smart pointer is an exception-safety panacea, a touchstone or amulet that by its mere presence somewhere nearby can help ward off compiler indigestion.
Exception safety is so tricky. I'm reminded of the nest of vipers that is JDBC exception handling, in which code which might encounter a JDBC exception tries to be a good citizen and clean up its resources, but the cleanup processing itself may encounter exceptions, and these exceptions may then conceal the original problem that led to the first exception, making your application an un-diagnosable mess.
Not to mention that all your code starts to look like this or this.
But now we've wandered far afield from C++ exception handling, and Sutter's great article (which you should read).
No comments:
Post a Comment