I have some code that I’ve inherited from someone very clever where they like to use gotos to leave the try block, jumping completely around the catch blocks.
It definitely works, and I suspect this is legal (I think that the C++ standard says that on exit from a scope, everything gets cleaned up properly, and I assume that applies to whatever the compiler had to do to implement exceptions on my platform).
Is this really legit? It’s NOT something I’d ever write (it’s too clever by half), but it’s clearly working, and I just want to understand why this is OK.
Even more specifically than the C++03 standard’s section on jump statements, it says this about try-blocks in the “Exception handling” clause (15/2):
C++11 contains the same wording.
Note however, that’s it’s not OK to jump into a try-block using a
goto(orswitch):