How do I make a call to what() on std::exception_ptr - c++

This is the code I have.
try
{
// code throws potentially unknown exception
}
catch (...)
{
std::exception_ptr eptr = std::current_exception();
// then what ?
}
Ideally, I would like to get the string associated to the exception if it is a std::exception.

// then what ?
here is what:
#include <exception>
#include <stdexcept>
#include <iostream>
#include <string>
std::string what(const std::exception_ptr &eptr = std::current_exception())
{
if (!eptr) { throw std::bad_exception(); }
try { std::rethrow_exception(eptr); }
catch (const std::exception &e) { return e.what() ; }
catch (const std::string &e) { return e ; }
catch (const char *e) { return e ; }
catch (...) { return "who knows"; }
}
int main()
{
try { throw std::runtime_error("it's success!"); }
catch (...) { std::cerr << "Here is WHAT happened: " << what() << std::endl; }
try { throw 42; } catch (...) { std::cerr << "and now what: " << what() << std::endl; }
}
what it prints:
Here is WHAT happened: it's success!
and now what: who knows
http://coliru.stacked-crooked.com/a/1851d2ab9faa3a24
so this allows to get what in the catch-all clause.
but what if exception is nested??? here is what:
std::string what(const std::exception_ptr &eptr = std::current_exception());
template <typename T>
std::string nested_what(const T &e)
{
try { std::rethrow_if_nested(e); }
catch (...) { return " (" + what(std::current_exception()) + ")"; }
return {};
}
std::string what(const std::exception_ptr &eptr)
{
if (!eptr) { throw std::bad_exception(); }
try { std::rethrow_exception(eptr); }
catch (const std::exception &e) { return e.what() + nested_what(e); }
catch (const std::string &e) { return e ; }
catch (const char *e) { return e ; }
catch (...) { return "who knows"; }
}
using example from here:
#include <fstream>
...
// sample function that catches an exception and wraps it in a nested exception
void open_file(const std::string& s)
{
try {
std::ifstream file(s);
file.exceptions(std::ios_base::failbit);
} catch(...) {
std::throw_with_nested( std::runtime_error("Couldn't open " + s) );
}
}
// sample function that catches an exception and wraps it in a nested exception
void run()
{
try {
open_file("nonexistent.file");
} catch(...) {
std::throw_with_nested( std::runtime_error("run() failed") );
}
}
int main()
{
try { throw std::runtime_error("success!"); }
catch (...) { std::cerr << "Here is WHAT happened: \"" << what() << '\"' << std::endl; }
try { run(); }
catch (...) { std::cerr << "what happened for run: \"" << what() << '\"' << std::endl; }
}
what is printed:
Here is WHAT happened: "success!"
what happened for run: "run() failed (Couldn't open nonexistent.file (basic_ios::clear))"
http://coliru.stacked-crooked.com/a/901a0c19297f02b5
but what if recursion too deep? what if stackoverflow? optimized what:
#include <typeinfo>
template <typename T>
std::exception_ptr get_nested(const T &e)
{
try
{
auto &nested = dynamic_cast<const std::nested_exception&>(e);
return nested.nested_ptr();
}
catch (const std::bad_cast &)
{ return nullptr; }
}
#if 0 // alternative get_nested
std::exception_ptr get_nested()
{
try { throw ; }
catch (const std::nested_exception &e) { return e.nested_ptr(); }
catch (...) { return nullptr ; }
}
#endif
std::string what(std::exception_ptr eptr = std::current_exception())
{
if (!eptr) { throw std::bad_exception(); }
std::string whaaat;
std::size_t num_nested = 0;
next:
{
try
{
std::exception_ptr yeptr;
std::swap(eptr, yeptr);
std::rethrow_exception(yeptr);
}
catch (const std::exception &e) { whaaat += e.what() ; eptr = get_nested(e); }
catch (const std::string &e) { whaaat += e ; }
catch (const char *e) { whaaat += e ; }
catch (...) { whaaat += "who knows"; }
if (eptr) { whaaat += " ("; num_nested++; goto next; }
}
whaaat += std::string(num_nested, ')');
return whaaat;
}
the same whats:
Here is WHAT happened: "success!"
here is what: "run() failed (Couldn't open nonexistent.file (basic_ios::clear))"
http://coliru.stacked-crooked.com/a/32ec5af5b1d43453
UPD
The similar functionality can be implemented in C++03 by using a trick that allows to rethrow current exception outside of catch block: https://stackoverflow.com/a/3641809/5447906

try
{
std::rethrow_exception(eptr);
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
http://en.cppreference.com/w/cpp/error/exception_ptr

Using std::current_exception seems a bit over the top in your case, since you don't seem to want to store or copy the std::exception_ptr for later processing (which is its only intent, it doesn't help with gaining additional information about an unknown exception in any way). If you just want to treat the case of a std::exception, what about the simple:
try
{
// code throws potentially unknown exception
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n'; // or whatever
}
catch (...)
{
// well ok, still unknown what to do now,
// but a std::exception_ptr doesn't help the situation either.
std::cerr << "unknown exception\n";
}

Not the best solution in my opinion but seems to work.
try
{
// code throws potentially unknown exception
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
catch (...)
{
std::exception_ptr eptr = std::current_exception();
// then what ?
LogUnknownException();
}
Thanks to ForEveR for the initial solution but I am not sure if I want to throw again within the catch block.

Related

How to get original exception back from std::exception once caught?

// Example program
#include <iostream>
#include <string>
#include <stdexcept>
void log(const std::exception& e) {
try {
throw e;
}
catch (const std::logic_error& e1) {
std::cout << "logic_error: " << e1.what() << std::endl; // How to get logic_error back once caught?
}
catch (const std::exception& e1) {
std::cout << "exception: " << e1.what() << std::endl;
}
}
int main()
{
try {
throw std::logic_error("sth wrong");
}
catch (const std::exception& e) {
log(e);
}
}
I don't want to add more catch clauses is because I want to have a central place to log detailed exception message, it could be very different across different exceptions.
Is there a way to narrow down the std::exception to derived exception in the catch clause?
You can rethrow the original exception in your log method to preserve its original state. This way everything should work as you would expect it.
Modified log method looks like this:
void log(const std::exception& e) {
try {
throw;
}
catch (const std::logic_error& e1) {
std::cout << "logic_error: " << e1.what() << std::endl;
}
catch (const std::exception& e1) {
std::cout << "exception: " << e1.what() << std::endl;
}
}
See throw expression for more details on how throw e and throw differs in this example.
std::exception::what() is virtual. So you don't need to get back the original type to write the logs. You just have to ensure that what() is redefined for your custom exceptions that inherits std::exception.
This may become:
void log(const std::exception & e)
{
std::cout << e.what() << std::endl;
}
You might use std::rethrow_exception:
void log(std::exception_ptr eptr) {
try {
if (eptr) {
std::rethrow_exception(eptr);
}
}
catch (const std::logic_error& e1) {
std::cout << "logic_error: " << e1.what() << std::endl;
}
catch (const std::exception& e1) {
std::cout << "exception: " << e1.what() << std::endl;
}
}
int main()
{
try {
throw std::logic_error("sth wrong");
}
catch (const std::exception&) { // or even catch (...)
log(std::current_exception());
}
}
You don't have to use only base exceptions provided by the standard library, you can make your own exception hierarchy with the methods you need.
#include <exception>
#include <string>
#include <iostream>
class MyBaseException: public std::exception
{
public:
virtual std::string output() const = 0;
/* Define methods you need here */
};
class MySpecificException : public MyBaseException
{
public:
const char* what() const noexcept override {return "error"; }
std::string output() const override { return "Specific debug output that I want"; }
};
int main(){
try{
throw MySpecificException{};
}catch(const MyBaseException& e){
std::cout << e.output() << '\n';
}catch(const std::exception& e) {
std::cout << e.what() << '\n';
}
}
You can use run time type information to get the type of the execption:
#include <iostream>
#include <string>
#include <stdexcept>
#include <typeinfo>
#if defined(__clang__) || defined(__GNUC__)
#include <cxxabi.h>
#endif
std::string getTypename(const std::exception& e)
{
#if defined(__clang__) || defined(__GNUC__)
// .name() returns a mangled name on gcc and clang
int status;
return abi::__cxa_demangle(typeid( e ).name(), 0, 0, &status);
#else
// visual studio returns a human readable name
return typeid( e ).name();
#endif
}
void log(const std::exception& e) {
std::cout << getTypename( e ) << ": " << e.what() << std::endl;
}
int main()
{
try {
throw std::logic_error("sth wrong");
}
catch (const std::exception& e) {
log(e);
}
}
The getTypename() method may not work on all platforms and you might need to adjust it to your needs. You might want to use boost::core::demangle rather than calling the gcc demangle API directly.

Print nested_exception with no nested_ptr

I'm trying to print nested exceptions using the following example code from cppreference.com:
void print_exception(const std::exception& e, int level = 0)
{
std::cerr << std::string(level, ' ') << "exception: " << e.what() << '\n';
try {
std::rethrow_if_nested(e);
} catch(const std::exception& e) {
print_exception(e, level+1);
} catch(...) {}
}
However, I get an abort if the innermost exception is a std::nested_exception rather than a std::exception (IE I throw a std::nested_exception, catch it, and then apply print_exception).
This is a minimal example:
int main() {
try {
std::throw_with_nested( std::runtime_error("foobar") );
} catch(const std::exception& e1) {
std::cerr << e1.what() << std::endl;
try {
std::rethrow_if_nested(e1);
} catch( const std::exception& e2 ) {
std::cerr << e2.what() << std::endl;
} catch( ... ) {
}
} catch ( ... ) {
}
}
It aborts:
foobar
terminate called after throwing an instance of 'std::_Nested_exception<std::runtime_error>'
what(): foobar
Aborted (core dumped)
The documentation for std::throw_with_nested states that
The default constructor of the nested_exception base class calls
std::current_exception, capturing the currently handled exception
object, if any, in a std::exception_ptr
so I would expect e1 to derive from std::nested_exception but have no nested_ptr. Why does std::rethrow_if_nested not handle this? What is the best approach for me to handle this case?
You may write something like:
// Similar to rethrow_if_nested
// but does nothing instead of calling std::terminate
// when std::nested_exception is nullptr.
template <typename E>
std::enable_if_t<!std::is_polymorphic<E>::value>
my_rethrow_if_nested(const E&) {}
template <typename E>
std::enable_if_t<std::is_polymorphic<E>::value>
my_rethrow_if_nested(const E& e)
{
const auto* p = dynamic_cast<const std::nested_exception*>(std::addressof(e));
if (p && p->nested_ptr()) {
p->rethrow_nested();
}
}
Demo

How to catch all custom exceptions in a single catch statement with Apache Thrift?

I have many different exceptions defined in exception.thrift:
exception InvalidArgumentsError {
1: string parameter
}
/**
* Server has an problem while executing a function.
* Execution aborted.
*/
exception ServerInternalError {
1: string parameter
}
/**
* Server answer is empty.
*/
exception NoDataError {
1: string parameter
}
This is how i catch them in my C++ code:
catch (InvalidArgumentsError & ex) {
std::cout << ex.parameter;
}
catch (ServerInternalError & ex) {
std::cout << ex.parameter;
}
catch (NoDataError & ex) {
std::cout << ex.parameter;
}
catch (apache::thrift::TException& ex) {
std::cout << "TException:\n" << ex.what();
} catch (const std::exception& ex) {
std::cout << ex.what();
return;
}
I want to write something like this and catch all my exceptions:
catch (SomeBasicException& ex) {
std::cout << ex.what();
}
catch (const std::exception& ex) {
std::cout << ex.what();
}
If I just catch TException and call what() I just get 'Default TException' message because derived classed do not override virtual what() method.
Generated code by thrift compiler:
class InvalidArgumentsError : public ::apache::thrift::TException {
public:
static const char* ascii_fingerprint; //..
static const uint8_t binary_fingerprint[16]; //..
InvalidArgumentsError() : parameter() {
}
virtual ~InvalidArgumentsError() throw() {}
std::string parameter;
_InvalidArgumentsError__isset __isset;
void __set_parameter(const std::string& val) {
parameter = val;
}
//...
};
If you want to stop the repetitive typing then this might be a solution.
When you want to catch your exceptions, you write something like this:
catch (...) {
handle_exception(print_message);
}
print_message is a function that does whatever you want with the message (in this case, print it):
void print_message(char const* const msg)
{
std::cout << msg;
}
handle_exception is written like this:
template<typename F>
void handle_exception(F handler)
try {
throw;
}
catch (InvalidArgumentsError const& ex) {
handler(ex.parameter.c_str());
}
catch (ServerInternalError const& ex) {
handler(ex.parameter.c_str());
}
catch (NoDataError const& ex) {
handler(ex.parameter.c_str());
}
catch (apache::thrift::TException const& ex) {
handler(ex.what());
}
catch (std::exception const& ex) {
handler(ex.what());
}
To handle a new exception you add it to the catch clauses of the handle_exception function.
Albeit this does not really answer the question literally (#Simple did a good job at this already) I would like to propose a somewhat different approach.
Given the code posted above:
exception InvalidArgumentsError {
1: string parameter
}
exception ServerInternalError {
1: string parameter
}
exception NoDataError {
1: string parameter
}
If we look at the pattern, we could - without loosing any information and without producing greater incompatibilities - change it into this:
enum ErrorCode
Success = 0, // always good to have some null value
ServerInternalError = 1
NoData = 2
InvalidArguments = 3
// add more errors here
}
exception MyServiceException {
1: string parameter
2: ErrorCode code
}
I'm fully aware that this might not fit your particular problem (e.g. if there are more exception data than just a 1:parameter). But there may be cases where that approach could be worth considering. I've used it myself quite a few times.

Unhandled exception, even after adding try-catch block ? C++

try
{
bool numericname=false;
std::cout <<"\n\nEnter the Name of Customer: ";
std::getline(cin,Name);
std::cout<<"\nEnter the Number of Customer: ";
std::cin>>Number;
std::string::iterator i=Name.begin();
while(i!=Name.end())
{
if(isdigit(*i))
{
numericname=true;
}
i++;
}
if(numericname)
{
throw "Name cannot be numeric.";
}
} catch(string message)
{
cout<<"\nError Found: "<< message <<"\n\n";
}
Why am I getting unhandled exception error ? Even after I have added the catch block to catch thrown string messages?
"Name cannot be numeric." is not a std::string, it's a const char*, so you need to catch it like this:
try
{
throw "foo";
}
catch (const char* message)
{
std::cout << message;
}
To catch "foo" as std::string you need to throw/catch it like this:
try
{
throw std::string("foo");
}
catch (std::string message)
{
std::cout << message;
}
You should send an std::exception instead, like throw std::logic_error("Name cannot be numeric")
you could then catch it with polymorphsim and the underlying type of your throw will no more be a problem :
try
{
throw std::logic_error("Name cannot be numeric");
// this can later be any type derived from std::exception
}
catch (std::exception& message)
{
std::cout << message.what();
}

Catch a throw exception with a custom string parameter

I'm trying to implement something similar to the following in my VS 2008 C++/MFC project:
class myClass()
{
public:
myClass()
{
//Do work... check for errors
if(var < 0)
{
//Error
TCHAR buff[1024];
_snwprintf_s(buff, SIZEOF(buff), L"ERROR: The value of var=%d", var);
throw buff;
}
}
};
__try
{
//Begin using class above
//The class member may be also defined on the global scale, thus
//the following exception trap may happen via SetUnhandledExceptionFilter
myClass mc;
}
__except(1)
{
//Process unhandled exception
//But how to trap the string passed in 'throw' above?
}
But I can't seem to catch the "string" that I'm passing in the throw statement.
Use std::runtime_error, eg:
#include <stdexcept>
class myClass()
{
public:
myClass()
{
//Do work...check for errors
if(var < 0)
{
//Error
char buff[1024];
_snprintf_s(buff, SIZEOF(buff), "ERROR: The value of var=%d", var);
throw std::runtime_error(buff);
}
}
};
try
{
//Begin using class above
myClass mc;
}
catch (const std::runtime_error &e)
{
//Process unhandled exception
//e.what() will return the string message ...
}
You can try something like this, for example:
struct CustomError {
CustomError(std::string& info)
: m_info(info) {}
std::string m_info;
};
/*...*/
int main() {
try {
std::string info = "yo dawg";
throw CustomError(info);
}
catch (CustomError& err) {
std::cout << "Error:" << err.m_info;
}
}
Character buffer way:
#include <string>
#include <iostream>
void DoSomething()
{
throw "An error occurred!";
}
int main()
{
try
{
DoSomething();
std::cout << "Nothing bad happened" << std:endl;
}
catch(const char &err)
{
std::cerr << err << std::endl;
}
}
, or std::string way:
#include <string>
#include <iostream>
void DoSomething()
{
throw std::string("An error occurred!");
}
int main()
{
try
{
DoSomething();
std::cout << "Nothing bad happened" << std:endl;
}
catch(std::string &err)
{
std::cerr << err << std::endl;
}
}