How to catch exceptions from assembler inserts?

For example, for such a code

#include <iostream> void foo() { _asm { xor ebx, ebx div ebx } } int main() { foo(); } 

I try to process it like this

 #include <iostream> void foo() { try{ _asm { xor ebx, ebx div ebx } } catch (...){ std::cout << "exception"; } } int main() { foo(); } 

, but the exception still falls.

Compiler: VC++ 2015

OS: windows

  • Since assembly inserts are system- and compiler-dependent, it makes sense to specify your compiler and operating system. - VladD
  • Actually, these are completely different exceptions ... And the C ++ exception mechanism has nothing to do with it. - Harry
  • @Harry so how to handle such exceptions? - ppp
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

You need to use system exception handling, C ++ has nothing to do with them. You can read more in Structured Exception Handling . Specifically for your code, it might look like this:

  __try { _asm { xor ebx, ebx div ebx } } __except(GetExceptionCode() == EXCEPTION_INT_DIVIDE_BY_ZERO ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) { std::cout << "exception"; }