Saturday, June 9, 2007

item 8

prevent exceptions from leaving destructors.

not prohibited in c++, but dont throw them,

imagine a vector v having 10 elements and v[1] destructor ( on destruction of v) throws an exception.

if your destructor cant avoid having to throw an exception ( imagine destructor of a class providing db connections--resource managing classes) than code it to either terminate the program or just swallow the exception whichever is suitable. Swallowing is considered bas compared to termination.

things to remember:
destructors should never throw exceptions. if functions called in a destructor may throw, the destructor should catch any exceptions then tswallow them or terminate the program

if class clients need to be able to react to exceptions thrown during an operation the class should provide a refular ( i.e. no ndestructor) function that performs the operation

No comments: