C ++ Does not finally support last-minute material management at the end of a block.
What happens when function C throws an exception, it returns from the function. This means that all local variables will be destroyed.
Then the program will return to the code in B , it will check the catch lock, see what is not, and return to the function A. Again, all the local variables in B will be freed.
You must remember that you are working in C ++ ... you must manage your objects. Therefore, if you have objects that are simply interested in freely in B (with pointers or something else), then this is a bad code design and they will not be released.
If you know in B that there may be an exception in C , you can just put catch there and then throw the exception.
Closest you can get to the finally block with catch(...) and use it to free memory ... but it will only be introduced if there really was an exception, so it is not identical to finally
try { } catch(...) {
If you want to work just like a finally block, you can do:
try { //Do stuff goto finally } catch(...) { finally: // free stuff affected by an exception here }
For all of you that have a problem with goto ..., it is still a command, and this is a great example that no break or continue or some built-in function can replace goto .
A little bit about go
Yochai timmer
source share