2

I'm writing unit tests using Boost.Test against some old C math library. One of tested functions in known to raise Integer Division By Zero system exception for some specified input. Let's say it's desired behavior and I want to write negative test for this case.

BOOST_REQUIRE_THROW(statement, exception); is not working for me as it is not C++ style exception (this macro is using try {} catch {} internally).

What is the correct way to handle case when I'm expecting failure on system level?

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
Mickey
  • 33
  • 6
  • If it's on Windows, you may use Structured Exception Handling (SEH) to catch div by 0. – Alexey Frunze Dec 12 '11 at 23:40
  • Yes, it's Windows. I could use SEH but I would prefer some Boost-based solution if there is any. – Mickey Dec 12 '11 at 23:47
  • @Mickey I do not think there is going to be an OS-independent solution because last I remember POSIX(Linux + Mac) does not allow you to continue after a divide by zero error. You would have to go through alot of effort in starting a separate process, etc. –  Dec 12 '11 at 23:51

1 Answers1

1

Since it's Windows, I suggest looking into _set_se_translator() Windows API. It allows to handle Structured Exception with C++ catch.

There are other possible ways, such is installing your handlers, but this one allows uniform exception handling as if they were C++ exceptions with minimal programming effort.

lapk
  • 3,838
  • 1
  • 23
  • 28
  • Ok, I think I will look deeper into this. I think I can get similar effect by setting "Enable C++ exceptions" to "Yes with SEH Exceptions (/EHa)" in Visual Studio project options. – Mickey Dec 13 '11 at 00:02