TITLE: exception cleanup control structures (Newsgroups: comp.lang.c++.moderated, 17 Jan 97) MUNSEY: Grant Munsey A stylistic problem I have always had with try/catch is that I seem to end up replecating the same cleanup code in both the catch block and after the catch block since most of the clean up code is the same and I very often rethrow the exception from within the catch block. Any suggestions on how to handle this, short of going to java which has a finally block? MEYERS: smeyers@teleport.com (Scott Meyers) Check out Jack Reeves' column in the January C++ Report. He discusses precisely this problem. The basic approach is to have a single catch clause containing common cleanup code, then use a new try block WITHIN THE HANDLER to rethrow the exception. That's followed by individual catch clauses to do the exception-specific handling. If you're programming with EH, you owe it to yourself to read Jack's past columns on the subject. They're very good. [ Here is some information and code adapted from the January '97 issue of "C++ Report". try { // possible exception here } catch (...) { // common cleanup code shared for all exceptions try { throw; // rethrow to catch below } catch (ExceptionTypeA& a) { } catch (ExceptionTypeB& b) { } } Here is an alternative structure (which I like better): try { try { // possible exception here } catch (...) { // common cleanup code shared for all exceptions throw; // rethrow to catch below } } catch (ExceptionTypeA& a) { } catch (ExceptionTypeB& b) { } -adc ]