Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get message of catch-all exception [duplicate]

If I want to write useful info to a file whenever i caught a catch-all exception, how to do it?

try {    //call dll from other company } catch(...) {    //how to write info to file here??????? } 
like image 269
5YrsLaterDBA Avatar asked Jun 28 '10 13:06

5YrsLaterDBA


People also ask

How do I get an exception message in C++?

C++ exception handling is built upon three keywords: try, catch, and throw. throw − A program throws an exception when a problem shows up. This is done using a throw keyword. catch − A program catches an exception with an exception handler at the place in a program where you want to handle the problem.

How do I get the detailed exception message in Python?

exc_info() . The function sys. exc_info() gives you details about the most recent exception. It returns a tuple of (type, value, traceback) .

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.


2 Answers

You can't get any information out of the ... catch block. That is why code usually handles exceptions like this:

try {     // do stuff that may throw or fail } catch(const std::runtime_error& re) {     // speciffic handling for runtime_error     std::cerr << "Runtime error: " << re.what() << std::endl; } catch(const std::exception& ex) {     // speciffic handling for all exceptions extending std::exception, except     // std::runtime_error which is handled explicitly     std::cerr << "Error occurred: " << ex.what() << std::endl; } catch(...) {     // catch any other errors (that we have no information about)     std::cerr << "Unknown failure occurred. Possible memory corruption" << std::endl; } 
like image 82
utnapistim Avatar answered Sep 29 '22 02:09

utnapistim


A caught exception is accessible by the function std::current_exception(), which is defined in <exception>. This was introduced in C++11.

std::exception_ptr current_exception(); 

However, std::exception_ptr is an implementation-defined type, so you can't get to the details anyway. typeid(current_exception()).name() tells you exception_ptr, not the contained exception. So about the only thing you can do with it is std::rethrow_exception(). (This functions seems to be there to standardize catch-pass-and-rethrow across threads.)

like image 36
Perette Avatar answered Sep 29 '22 02:09

Perette