0

Continued from my previous question: How to handle SEH exception on a global level

Does Vector Exception Handlers catch all exceptions including C++ exceptions? Microsoft's documentation says it is an extension of SEH handlers but I read somewhere that it's not possible to catch all c++ exceptions in different threads globally since they have their own stack, which of them is true?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Batmon
  • 21
  • 5
  • [SEH can already handle C++ exceptions](https://learn.microsoft.com/en-us/cpp/cpp/structured-exception-handling-c-cpp?view=msvc-170), so, yes, an extension to SEH should also do that. What do you mean by "all exceptions" and "different threads" and "all exceptions in different threads" and "globally"? How many exceptions do you want to catch and why would you want to catch them on a different thread? Do you want to throw an exception on one thread and catch it on another? What should happen to the thread that threw the exception? – Thomas Weller Jun 05 '23 at 17:45
  • I want to catch any exception occurring in a different thread, can vector exception handler in Main catch that? – Batmon Jun 05 '23 at 17:47
  • What are you planning to do? catching SEH's in C++ code should only be done at top level (never locally). I've seen developers happily catch access violations and then continue. That said, exception handling is stack based, each thread has its own stack so also its own exception handling. Not catching an exception on a thread my result in the application being terminated. – Pepijn Kramer Jun 05 '23 at 17:49
  • Note that vc++ uses only SEH exceptions for C++ while mingw may opt to Dwarf or SJLJ exceptions. Anyway, this whole idea of handling C++ exceptions from different threads sounds like an XY problem. – user7860670 Jun 05 '23 at 17:56
  • VEHs are thread-specific. Although technically possible, do not assume you can use them to handle an exception. There isn't enough context to do it right. – Hans Passant Jun 05 '23 at 18:04
  • Assume there is a runtime exception (or any other c++ exception) in a thread of a multi-threaded program, will VEH in main able to catch it? I just want to delay (i.e. sleep) the thread with the exception for logging purposes. Sorry I am confused with different responses. – Batmon Jun 05 '23 at 18:16
  • @Batmon `main()` can *register* a VEH handler, but the handler will not *run* in the same thread as `main()` if a SEH is raised by another thread. Have a look at [Under the Hood: New Vectored Exception Handling in Windows XP](https://bytepointer.com/resources/pietrek_vectored_exception_handling.htm) for details. – Remy Lebeau Jun 05 '23 at 19:12
  • Thanks @RemyLebeau, I also wanted to ask if \EHa compiler flag is required for catching SEH exceptions as we need with normal SEH handlers? – Batmon Jun 06 '23 at 08:25

1 Answers1

0

I think you're kind of getting things backwards.

With Microsoft's compiler, you can covert structured exceptions into C++ exceptions, and catch them as such (but at least in my experience, this isn't particularly useful in practice).

I haven't tested that with vectored exceptions but they're similar enough that I'd guess you can probably do the same with them (but I'd expect the same lack of good results).

I don't know of anything that allows you to go the other direction though. You can implement C++ exceptions in terms of structured exceptions if you want. Since vectored exceptions are an extension of structured exceptions, you can implement C++ exceptions in terms of vectored exceptions as well.

But there's no guarantee that any particular compiler will actually do that though. Microsoft definitely did at one time, but it's not at all clear to me that they do any more. For example, the compiler has two switches:

/EHs enable C++ EH (no SEH exceptions)  /EHa enable C++ EH (w/ SEH exceptions)

Maybe /EHs could still use SEH internally, and just not provide them at the user level, but it's not at all certain. I seem to have a vague recollection that it doesn't, but I'm not at all sure.

gcc and clang do now have at least some support for SEH (and probably VEH), but at least the last time I looked, they didn't depend on either for their implementation of C++ exceptions.

Bottom line: I doubt anybody really guarantees that what you want will work, so about the best you can hope for is to test, and perhaps it'll work for the code you care about with some specific compiler and flags. But at best it's likely to be fairly fragile--a seemingly minor change in the compiler or even just flags could break it.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • So it's dependent on \EHa compiler flag? – Batmon Jun 05 '23 at 18:53
  • @Batmon: It probably depends on /EHa vs /EHs. It may depend on other things as well though. Like I said, pretty much the only choice here is to test, and realize that the result of the test can become invalid with any compiler upgrade, change of compiler flags, or much of anything else. – Jerry Coffin Jun 05 '23 at 18:56