Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get some information from exception caught with catch(...)? [duplicate]

I have a try catch clause where the outermost catch(...) never happened until now. After some changes, somewhere an exception which I don't handle with the other cases is thrown. Is there a way to get at least some information about the exception even though I catch it with (...)?

catch (const cone::BeginnersLibException& ex) 
{
    // handle the exception
}
catch (const std::exception& ex) 
{
    // handle std exception
} 
catch (...) 
{
    log("Unknown exception caught.");
    // How can I get more information about this exception?
}

Edit: here a code snippet that works for me:

#include <cxxabi.h>

// more code here 
} catch (...) {
    std::string exName(abi::__cxa_current_exception_type()->name());
    std::cout<<"unknown exception: "<< exName <<std::endl;
    throw;
}
like image 335
Beginner Avatar asked Feb 16 '15 09:02

Beginner


People also ask

Does catch exception catch everything?

If you catch some Exception types and don't do anything with the information, you have no chance of knowing what went wrong in those situations, but if you catch all Exception subclasses you have no chance of knowing what went wrong in a much large number of situations.

What is caught exception?

Each catch block is an exception handler that handles the type of exception indicated by its argument. The argument type, ExceptionType , declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable class. The handler can refer to the exception with name .

What happens when an exception is caught C++?

C++ try catch and throw When an exception occurs within the try block, control is transferred to the exception handler. If no exception is thrown, the code continues normally and the handlers are ignored.

How do you handle multiple exceptions?

By handling multiple exceptions, a program can respond to different exceptions without terminating it. In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.


1 Answers

You can do this using gdb or another debugger. Tell the debugger to stop when any exception is throw (in gdb the command is hilariously catch throw). Then you will see not only the type of the exception, but where exactly it is coming from.

Another idea is to comment out the catch (...) and let your runtime terminate your application and hopefully tell you more about the exception.

Once you figure out what the exception is, you should try to replace or augment it with something that does derive from std::exception. Having to catch (...) at all is not great.

If you use GCC or Clang you can also try __cxa_current_exception_type()->name() to get the name of the current exception type.

like image 180
John Zwinck Avatar answered Oct 03 '22 02:10

John Zwinck