| ▲ | Can I throw a C++ exception from a structured exception?(devblogs.microsoft.com) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 points by birdculture 4 days ago | 13 comments | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | amelius 5 hours ago | parent | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
For context: https://learn.microsoft.com/en-us/cpp/cpp/structured-excepti... > Structured exception handling (SEH) is a Microsoft extension to C and C++ to handle certain exceptional code situations, such as hardware faults, gracefully. Although Windows and Microsoft C++ support SEH, we recommend that you use ISO-standard C++ exception handling in C++ code. It makes your code more portable and flexible. However, to maintain existing code or for particular kinds of programs, you still might have to use SEH. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | xerxes901 3 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Hm. I found this (that memory must be stable wherever a SEH exception could be thrown) surprising, because I thought the unwind information generated by the compiler should be able to reconstruct all the correct variable values during stack unwinding. TIL | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | rramadass 4 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
More details; Mixing C (structured) and C++ exceptions - https://learn.microsoft.com/en-us/cpp/cpp/mixing-c-structure... Handle structured exceptions in C++ - https://learn.microsoft.com/en-us/cpp/cpp/exception-handling... For even more fun, Microsoft C++ implementation of setjmp/longjmp calls dtors of lexically scoped objects properly during stack unwinding (when compiled with proper switches) - https://learn.microsoft.com/en-us/cpp/cpp/using-setjmp-longj... Finally Important caveats from - https://learn.microsoft.com/en-us/cpp/build/reference/eh-exc... Specifying /EHa and trying to handle all exceptions by using catch(...) can be dangerous. In most cases, asynchronous exceptions are unrecoverable and should be considered fatal. Catching them and proceeding can cause process corruption and lead to bugs that are hard to find and fix. Even though Windows and Visual C++ support SEH, we strongly recommend that you use ISO-standard C++ exception handling (/EHsc or /EHs). It makes your code more portable and flexible. There may still be times you have to use SEH in legacy code or for particular kinds of programs. It's required in code compiled to support the common language runtime (/clr), for example. For more information, see Structured exception handling. We recommend that you never link object files compiled using /EHa to ones compiled using /EHs or /EHsc in the same executable module. If you have to handle an asynchronous exception by using /EHa anywhere in your module, use /EHa to compile all the code in the module. You can use structured exception handling syntax in the same module as code that's compiled by using /EHs. However, you can't mix the SEH syntax with C++ try, throw, and catch in the same function. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | quotemstr 5 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
C++ exceptions are SEH exceptions with a reserved opcode though. So the answer is "yes", "no", and "obviously" depending on how knowledgeable you are about the platform. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ▲ | im3w1l 7 hours ago | parent | prev [-] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
> No, that’s not why the /EHa option results in less efficient code. The possibility that any memory access or arithmetic operation could trigger an exception significantly impairs optimization opportunities. It means that all variables must be stable at the point memory accesses occur. This is a good insight but I feel like stopping the analysis here is a little bit too early. We should also think about what they actually wanted to achieve. Did they actually need all variables to be stable at the point of any memory access? Maybe they want 90% of the benefits at 10% of the cost somehow? | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||