Exception sometimes not caught using C++ / GCC / Linux - c++

Occasionally I encounter a piece of code where an exception is not caught. So far I have no idea what a possible reason can be.
As an example the piece of code below.
The log message "initializing" is printed.
Some log messages from module->Init() are printed
module->init() throws an exception
Log messages in the catch blocks are not printed
Application terminates
log::trace( "Initializing" );
try
{
module->Init();
}
catch( const std::exception& e )
{
log::error( "Error initializing module:{}", e.what());
}
catch( ... )
{
log::error( "Unknown exception initializing module" );
}
dmesg:
[ 9.557049] terminate called after throwing an instance of '
[ 9.562655] std::runtime_error
[ 9.565715] '
[ 9.567350] what():
followed by the error message passed to what() which was the generated exception message in init.
Exception handling works as expected in other parts of the same application.
Exception is thrown from the main thread.
Embedded ARM platform with build system. All libraries are compiled with the same compiler.
Compiler used GCC 8.2.0
Is there anything I am not aware of, which might cause an exception not to be caught ?

Actually overlooked something in the header file. The function was marked "noexcept" which actually caused a terminate when an exception was thrown.

Related

SIFT detectAndCompute throws an ipp exception

In my real time image tracking solution, whenever i am calling detectAndCompute i get an exception thrown.
The exception doesnt crash the program, and the tracking still works (on my machine) but due to the fact that it is constantly throwing the exception, I am seeing some major performance setbacks. Here is the exception:
Exception thrown at 0x00007FFF0FBEA839 in OpenCV2.exe: Microsoft C++ exception: ipp::IwException at memory location 0x0000004CB72FC5C8.
I tried printing the exception to get details with a try-catch clause but it didnt give me any info. Here is the line that throws this exception:
algo->detectAndCompute(frame, mask, keypoints2, descriptors2, false);
This is harmless, see for example https://github.com/opencv/opencv/issues/9718.
If you run your code outside of the debugger you will not see the exception printout.

How to get CLion to show exceptions?

I have CLion installed with presumably default configuration. I think something is wrong with it, because I can't see exceptions. For example, this code:
int main(){ throw 5; }
Prints only Process finished with exit code 0
Why doesn't it print the exception?
Why does it print 0 instead of 1?
For comparison:
int main(){try { throw 5; } catch(int x) { std::cout << x << '\n'; }}
This prints 5, so it looks like code is correctly run and the exception is correctly thrown. It's just hidden by CLion somehow.
Edit: This is not a duplicate of "not seeing any console output". I made extremely clear in my question that I was indeed seeing console output for prints. My issue concerns exceptions specifically, not console output generally.
In your first piece of code you have not caught the exception, so it is handled by the default handler. You therefore have no control over the return code from the executable. Operation is as expected.
If you would like CLion to display the exception you can configure it to do so. Note that this will only apply when CLion is debugging your executable, outside of CLion your executable will continue to behave as you have already seen.
Go to run then view breakpoints.
Go to exception breakpoints and when any is thrown.
To display the stack trace when the exception is thrown check stack trace
If you would like your program to stop for your intervention, check enabled and when thrown.
Make sure to use Run/Debug and not Run/Run to launch your program.

What is a R6010 error?

I ran into a problem whereby my executable may receive an abort/retry/ignore dialog such as:
Debug Error!
Program: ...whatever.exe
R6010
- abort() has been called
I believe it is because I have an unhandled exception and I can replicate the problem with this simple program:
int _tmain(int argc, _TCHAR* argv[])
{
try
{
throw std::exception();
}
catch (std::logic_error& e)
{
}
std::cout << "Hello World!";
return 0;
}
Is my assumption correct (it is caused by an unhandled exception)? I have found plenty of examples online for the error but nothing that actually defines what the error code means.
This error only occurs in my debug build. My release build will just hang before crashing out (which is what I expect for the unhandled exception).
This happens if there's an assertion: assert( condition );.
Assertions are only checked/compiled in the debug build.
Unhandled exceptions make assertions internally, in order to allow the developer to interrupt and debug the program and to find the error.
In release build, the application crashes immediately.
If you remove or catch the exception, the error will not appear anymore.

gdb - Prevent losing backtrace in a catch/rethrow situation

Is it possible to re-throw an exception without losing the back-trace in gdb? Or is there a way in gdb to "back up' a few lines and back trace from there? I'm on GDB 7.7.1, the most recent.
I sometimes find myself running into situations like this, needing a back trace from the original throw of the exception, and needing to comment out the try/catch parts, recompiling, and re-running in gdb.
try {
someFuncThatCanThrowException();
} catch(exceptionType& exception) {
if(#CAN_RECOVER#) {
...
} else {
throw;
}
}
----OR----
try {
someFuncThatCanThrowException();
} catch(exceptionType& exception) {
exception.printMessageToCout();
throw;
}
needing a back trace from the original throw of the exception,
Is it OK to use a simple approach of printing all backtraces of all throws and then when it is necessary to find a backtrace of a particular exception just find it by the address of the exception. Something like this sequence of gdb commands:
set pagination off
catch throw
commands
info args
bt
c
end
When you need to find backtrace of an exception, first print its address, like this:
print &exception
And find its address in the gdb output. It must be printed by info args. As soon as you find address there will be backtrace of this exception after info args output.

Throw Causes Program to Crash Instead of going to the Catch Block

I am using NetBeans 7.0.1 on Ubuntu 11.10.
I set set up a
try
{
int error;
// Code
if (error) throw error;
}
catch(int errorNumber)
{
// Error handling
}
block. However, when an exception is thrown, I get the following "Signal Caught" box instead of the program going to my catch block.
Signal received: SIGABRT (?) with sigcode ? (?)
From process: ?
For program celltowebimage, pid 30,222
You may discard the signal or forward it and you may continue or pause the process
To control which signals are caught or ignored use Debug->Dbx Configure
Every other time a program I write throws an exception, it goes to the catch block. Why is NetBeans crashing the program instead? An example of the exception number that is thrown is -23. However they are all small, negative numbers and all result in the same problem.