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??????? }
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.
exc_info() . The function sys. exc_info() gives you details about the most recent exception. It returns a tuple of (type, value, traceback) .
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.
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; }
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With