https://gcc.gnu.org/bugzilla/show_bug.cgi?id=38658
Martin Sebor <msebor at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|RESOLVED |UNCONFIRMED Resolution|INVALID |--- Summary|inefficient code on trivial |trivial try/catch statement |try/catch statement |not eliminated --- Comment #3 from Martin Sebor <msebor at gcc dot gnu.org> --- There are situations when throwing an exception is required to result in a call to std::terminate() but those require the exception to escape the function where it's thrown from. Outside of those, it seems to me that as long as the observable effect of the optimized code is indistinguishable from the original (such as in the test case) removing try/catch statements should be a valid optimization. Allocating memory is not an observable effect of throwing an exception so it doesn't count. std::uncaught_exception() has to be called along the way so that is also out to the extent that GCC can see the code between the throw expression and the catch block. It's clear that not all cases can be optimized as I suggest but I believe at least some of them can (the one in the test case in comment #0). I'm going to reopen this request. If it turns out that there's something I'm missing we can close it once it's made clear in a test case that the optimization would either be invalid even in that subset of cases, or if it looks like it would not be profitable enough to bother.