Quantcast
Channel: Does C++ support 'finally' blocks? (And what's this 'RAII' I keep hearing about?) - Stack Overflow
Viewing all articles
Browse latest Browse all 17

Answer by bcmpinc for Does C++ support 'finally' blocks? (And what's this 'RAII' I keep hearing about?)

$
0
0

Not really, but you can emulate them to some extend, for example:

int * array = new int[10000000];try {  // Some code that can throw exceptions  // ...  throw std::exception();  // ...} catch (...) {  // The finally-block (if an exception is thrown)  delete[] array;  // re-throw the exception.  throw; }// The finally-block (if no exception was thrown)delete[] array;

Note that the finally-block might itself throw an exception before the original exception is re-thrown, thereby discarding the original exception. This is the exact same behavior as in a Java finally-block. Also, you cannot use return inside the try&catch blocks.


Viewing all articles
Browse latest Browse all 17

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>