Throw Causes Program to Crash Instead of going to the Catch Block - c++

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.

Related

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

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.

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.

How to set breakpoint in catch block? (c++)

There is something strange happening when I try to debug application. Simply the debugger does not stop on breakpoints when I set breakpoints in catch portion of try-catch block.
Here is an example.
try {
throw std::overflow_error("test");
} catch (...) {
qDebug() << "caught"; // HERE, I SET BREAKPOINT ON THIS LINE
}
It prints the "caught" on screen when exception occurs but it does not stop on this line. (If you ever wonder; Yes, I'm building app in Debug mode and running in Debug mode)
Am I suffering from lack of fundamental knowledge about how gdb works? (I mean maybe it does not stop because breakpoints in catch portion does not work)
Any help would be greatly appreciated.
Thanks.
To catch an exception in IDE, you need issue gdb commands directly in gdb console.
Here's link how to get into gdb console in Qt Create IDE:
Accessing gdb console in Qt-Creator
Once you're the type
catch throw
to stop when your program throws an exception or
catch catch
to stop in the catch block.
If you need to catch a specific library exception, read this thread: GDB: How to break when a specific exception type is thrown?
for people using LLDB,
# set on both throw and catch
breakpoint set -E C++ -h true
# or on catch
b __cxa_begin_catch
# or on throw
b __cxa_throw
while will set breakpoints on throw and catch.
#ben sen, I guess any opinization may result in such a behavior. There are many ways how those options can be specified (via environment variables aka CFLAGS or via IDE options to the project), but they all result to some particular -O option given to the compiller command line. Even if nothing is given at all, please check what is the default optimization for your compiller. My proposal would be to explicitely give -O0 to the compiller and check that no other -O options are supplied.

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.