rethrow exception preserving backtrace

c# rethrow exception preserve stack trace
c# rethrow exception with new message
catch and rethrow java
vb.net rethrow exception
c# throw
c# throw exception
c# throw vs throw ex
rethrow c++

I need to catch some "fatal" C++ exception, then flush logs and rethrow the former, with its own backtrace.

My current solution, however, displays (correctly) the wrong stacktrace.

#include <exception>
#include <iostream>

void fatal(const std::exception & E)
{
    // do - something - extremely - important
    throw E;
}

int foo()
{
    throw std::runtime_error("yet another foo function");
}

int main()
{
    try
    {
        return foo();
    }
    catch (const std::exception & E)
    {
        fatal(E);
    }
    return -1;
}

Program being wrapped by

  $ cat ./backtrace
  backtrace
  quit

  $ ulimit -c unlimited
  $ ./a.out
  $ gdb -q ./a.out core -x ./backtrace

Result is

Program terminated with signal SIGABRT, Aborted.

..................................................

4 0x00007f496eb53701 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6

5 0x00007f496eb53919 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6

6 0x0000000000400d71 in fatal(std::exception const&) ()

7 0x0000000000400e5b in main ()

I thought rethrowing an exception (by const ref) was a technique to pass the original backtrace; I'm interested in backtracing foo(), not fatal().

With your script:

backtrace
quit

... you will only see a stack trace when the inferior is about to exit (or when you use a core file, like in your example, when it has already exited), because you have not told gdb to stop anywhere.

Another approach would be to use the gdb catch throw command, plus a bit of scripting. This way you could capture a stack trace at each throw. You could do it this way:

(gdb) catch throw
Catchpoint 1 (throw)
(gdb) commands
Type commands for breakpoint(s) 1, one per line.
End with a line saying just "end".
> silent
> backtrace
> continue
> end

This will stop at every throw and print a backtrace. However, you did want to not print stack traces from the fatal frame. For this, you could use a gdb convenience function and make the catchpoint conditional:

(gdb) cond 1 $_any_caller_matches("fatal", 10)

(The "10" is just a guess at how many frames may separate fatal from the C++ library innards that handle throwing.)

CA2200: Rethrow to preserve stack details, If an exception is re-thrown by specifying the exception in the throw statement, the stack trace is restarted at the current method and the list of  To keep the original stack trace information with the exception, use the throw statement without specifying the exception. How to fix violations. To fix a violation of this rule, rethrow the exception without specifying the exception explicitly. When to suppress warnings. Do not suppress a warning from this rule. Example

I'm not sure what you are trying to accomplish here, but to rethrow an exception you need to write throw; (without arguments), throw E; will throw a new exception instance by copy constructing it from existing object.

Not sure how any of these may help you with stack traces though. "pass the original backtrace" does not make much sense because exceptions in C++ do not carry any backtraces and by the time your catch block is invoked stack has been already cleanuped.

Rethrowing exceptions and preserving the full call stack trace, Did you know that depending on the way you rethrow exceptions you may lose important information? There are already several blog posts that  Preserving a stacktrace when rethrowing exception with ExceptionDispatchInfo One of the annoyances with Exception handling in C# is that it is easy to stomp the stacktrace. Now everyone who has been in.NET for a while knows you can call throw inside a catch block to rethrow the exception properly

This question has already been answered, but I want to add that one can create proper backtraces in standard C++11:

Use std::nested_exception and std::throw_with_nested

It is described on StackOverflow here and here, how you can get a backtrace on your exceptions inside your code without need for a debugger or cumbersome logging, by simply writing a proper exception handler which will rethrow nested exceptions. This requires, however, that you wrap all functions you wish to trace into try/catch blocks, but it lets you do a lot of customisation of what happens and what information is printed. Since you can do this with any derived exception class, you can add a lot of information to such a backtrace!

You may also take a look at my MWE on GitHub or my "trace" library, where a backtrace would look something like this:

Library API: Exception caught in function 'api_function'
Backtrace:
~/Git/mwe-cpp-exception/src/detail/Library.cpp:17 : library_function failed
~/Git/mwe-cpp-exception/src/detail/Library.cpp:13 : could not open file "nonexistent.txt"

How to rethrow exception correctly in .Net, Such a pattern of rethrowing the exception can turn an exercise of One other way to preserve the stack trace information of the original exception on rethrow is​  The complete way to rethrow a caught exception is to use ExceptionDispatchInfo.Capture( ex ).Throw() (only available from .Net 4.5). Below there are the cases necessary to test this: 1. void CallingMethod() { //try { throw new Exception( "TEST" ); } //catch { // throw; } } 2.

Preserving a stacktrace when rethrowing exception with , One of the annoyances with Exception handling in C# is that it is easy to stomp the stacktrace. Now everyone who has been in .NET for a while  When you throw an exception using “throw ex” instead of “throw” you override the original stack trace with a new stack trace that starts from the throwing method. This can make tracking down the root causes of exceptions much more difficult.

Rethrowing exceptions in Java without losing the stack trace , In C#, I can use the throw; statement to rethrow an exception while preserving the stack trace: (that doesn't lose the original stack trace)? How to rethrow exception correctly in .Net. A few days ago I stumbled across a C# code that was rethrowing the exception by means of passing captured exception object as an argument of the throw keyword (throw ex;). Such a pattern of rethrowing the exception can turn an exercise of troubleshooting a production issue into a game of “Find where the exception happened.”.

How to rethrow an exception in JavaScript, but preserve the stack , Stack trace conveys some portion of the data whenever an exception is thrown. The stack trace is a collection of all the methods used in the program. It starts  For instance, throwing an exception using jl_throw records the backtrace into a raw ip buffer attached to the thread local storage. This is a bare minimum of raw information for efficiency and needs to be further converted into julia-level information when calling catch_backtrace and associated functions.

Comments
  • Nice, i throwed inside the catch block, giving up with fatal() scoping... and the backtrace is as expected
  • There is nothing in standard C++ that associates an exception with a "backtrace". Whatever you're describing, it is specific to one implementation. You'll need to read documentation for that implementation. Incidentally, to rethrow an exception within an exception handler, all you need to do is throw; - there is no need to explicitly rethrow an exception that has been caught. Bear in mind that calls terminate() if there is no active exception to rethrow.