2

I am used to programming in Delphi. In that system, a hardware floating point exception, e.g. division by zero, overflow, etc. results in a native Delphi exception being raised. The mechanism is that the hardware exception are not masked and those hardware exceptions are converted, by the Delphi RTL, into native Delphi exceptions.

My app has a plug-in architecture and many of my clients write plug-ins using the MS C++ compiler. When a floating point error occurs in the client's C++ plug-in code this jumps out of the C++ code and into an exception handler in my Delphi code. This makes it difficult for the plug-in author to diagnose where in their code the problem is because, so far as I can tell, they cannot trap these exceptions since they are not C++ exceptions.

Is there any way that I can set some option in the C++ code to make the C++ compiler convert floating point exceptions into C++ exceptions? Or do I have to mask the exceptions in the C++ code and test for errors manually?

And as an aside, how does one go about writing robust scientific and numerical code if the standard C++ tools ignore floating point errors? It seems like a glaring weakness, or am I missing something?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    see http://stackoverflow.com/questions/4747934/c-catch-a-divide-by-zero-error or http://stackoverflow.com/questions/4795169/handling-cpu-exceptions-in-c – rve Oct 28 '11 at 08:31
  • An example to catch floating point exceptions in C++ here (http://www.cplusplus.com/forum/beginner/51694/) – LU RD Oct 28 '11 at 08:40
  • @LURD That just throws a float and catches it, so far as I can tell. There's no FP exception there. – David Heffernan Oct 28 '11 at 08:43
  • 1
    Ok, look here then : [Dealing with Floating-point Exceptions in MSVC7\8](http://www.devx.com/cplus/Article/34993/1954) – LU RD Oct 28 '11 at 08:49
  • @LURD That looks promising. I'll pursue that. Thanks. – David Heffernan Oct 28 '11 at 08:51

2 Answers2

3

How about this example converting SEH into C++ exceptions?

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • Thanks for that. Same link as MSalters as it happens. – David Heffernan Oct 28 '11 at 09:41
  • Yeah, experimenting took too long and I didn't see his. [Here](http://blogs.msdn.com/b/jaredpar/archive/2008/01/11/mixing-seh-and-c-exceptions.aspx) are a few details. – Alexey Frunze Oct 28 '11 at 10:21
  • Alex; Thanks for that link. As a delphi programmer who escaped from C++ about 10 years ago, I was reminded of why I was so happy to leave behind C++. Grins. – Warren P Oct 28 '11 at 18:36
  • @WarrenP: I can totally relate... Though, in my case it's the other way around. :) Maybe it's just that so far I've been fortunate enough to do much more of plain C than C++. – Alexey Frunze Oct 28 '11 at 19:57
2

Yes, the relevant function is _set_se_translator

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Thanks. I'll read up on that. I guess I need to learn about SEH. And presumably I use the return value from `_set_se_translator` to set up a chain of SEH translators. And also restore the previous translator before the DLL returns. – David Heffernan Oct 28 '11 at 09:40