Destructors Should Handle Exceptions Locally

It is legal to call a function that may throw an exception inside a destructor. However, it is important to handle any exception that might be thrown from such a function inside the destructor body.

 void cleanup() throw (int);class C {public:  ~C();};C::~C() {  try {    cleanup();  }  catch(int) {    //handle the exception  }}

The exception thrown by cleanup() is handled inside the destructor. Otherwise, the thrown exception will propagate outside the destructor, and if the destructor has been invoked while unwinding the stack due to another exception, terminate() will be called.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: