0

In one of my C++14 programs I encountered exception behaviour that I do not understand / did not expect. Can somebody please enlighten me?

I have an abstract baseclass which looks like that (simplified): There is a method "templateLoop" which encapsulates over try/catch all block the abstract method doReadStat which is implmeneted in dervied classed. There is also a start method which executed the templateLoop in a thread.

class BaseStatReader{    

virtual void doReadStat()=0;
    
void templateLoop() {
    try {
        doReadStat();
    }
    catch (...){
        //cout ...
    }        
}

void start(){
    t = std::thread([&](){ templateLoop();});
}

std::thread t;
   

};

Now I have a concrete derived class (lets call it ConcreteStatReader) which implments doReadStat. In the application code I instantiate an object of ConcrteStatReader and call start on it. Now within this concrete implementation of doReadStat() a floating point runtime exception occurs. To my understanding it should be caught in the baseclass templateLoop catch(...) block which it does not. The program terminates, the exception is never caught. why?

rejuce
  • 1
  • 1
  • 2
    A "floating point runtime exception" is not a C++ exception. It is undefined behavior, or a bug. C++ exceptions are not a built-in solution for undefined behavior, or bugs. – Sam Varshavchik Jul 19 '20 at 20:38
  • I see. what would be a good approach to "catch" such? so at least one can tell from which component/thead of the application it originated? – rejuce Jul 19 '20 at 20:41
  • This is done by using a debugger to trap the exception, then inspect the state of the program and analyze it, to determine the reason for the bug, and then fix the bug. – Sam Varshavchik Jul 19 '20 at 20:42
  • Of course if that's a possibility. I had the problem though that the exception did not occur on any of my systems and I could not reproduce the issue on any where I could debug on, just on some deployed systems with text output. It was a pain to find the origin and I wondered if it can be done better... – rejuce Jul 20 '20 at 06:23

0 Answers0