Search This Blog

Sunday, October 25, 2009

C++, Exception Safe Function

Exception Safe Code means:
1. Leak no resources: easily achieve with share_pointer
2. Data structure is not corrupted, one of the following must be offered.
a. Basic Guarantee: try ... catch to help a function either success or throws an exception, and leaks no resources. e.g. STL offers basic guarantee.
b. Strong Guarantee: atomic, in addition to basic guarantee. usually achieved by working on temporary, and then swap. P.680, Programming
c. Nothrow Guarantee: never throw exception. Avoid throw, new, and dynamic_cast of reference type. working on built-in type like int, pointers, etc.

Usually, basic and strong guarantee are useful proving correctness of your program. To implement them, often use RAII -- Let object to manage your resources.

No comments: