C++11 exception features


#include <exception>

typedef /*unspecified*/ exception_ptr;

template< class E >
std::exception_ptr make_exception_ptr( E e )

std::exception_ptr current_exception();

[[noreturn]] void rethrow_exception( std::exception_ptr p );




#include <iostream>
#include <string>
#include <exception>
#include <stdexcept>

void handle_eptr(std::exception_ptr eptr) // passing by value is ok
{
    try
    {
        if (eptr != std::exception_ptr())
        {
            std::rethrow_exception(eptr);
        }
    }
    catch(const std::exception& e)
    {
        std::cout << "Caught exception \"" << e.what() << "\"\n";
    }
}

int main()
{
    std::exception_ptr eptr;
    try
    {
        std::string().at(1); // this generates an std::out_of_range
    }
    catch(...)
    {
        eptr = std::current_exception(); // capture
    }
    handle_eptr(eptr);
} // destructor for std::out_of_range called here, when the eptr is destructed


// output: Caught exception "basic_string::at"




std::nested_exceptions is a polymorphic mixin class which can capture
and store the current exception, making it possible to nest exceptions
of arbitrary types within each other.


class nested_exception
{
public:
  nested_exception();
  ~nested_exception();
  nested_exception& operator=(const nested_exception&);

  [[noreturn]] void rethrow_nested() const;
  std::exception_ptr nested_ptr() const;
};


template< class T >
[[noreturn]] void throw_with_nested( T&& t );

template< class E >
void rethrow_if_nested( const E& e );
{
// If E is a type that is unambiguously derived from std::nested_exception, 
//   extracts and throws the nested exception as if by calling 
//   dynamic_cast<const std::nested_exception&>(e).rethrow_nested(). 
// Otherwise, does nothing.
}

#include <iostream>
#include <stdexcept>
#include <exception>
#include <string>
#include <fstream>

// prints the explanatory string of an exception. If the exception is nested,
// recurses to print the explanatory of the exception it holds
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(...) {}
}

// 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") );
    }
}

// runs the sample function above and prints the caught exception
int main()
{
    try
    {
        run();
    }
    catch(const std::exception& e)
    {
        print_exception(e);
    }
}


// output:
// exception: run() failed
//  exception: Couldn't open nonexistent.file
//   exception: basic_ios::clear