[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/error/throw_with_nested [Back]  [Original]

std::throw_with_nested cppreference.com
cppreference.com

std::throw_with_nested

cppreference.com
<tbody> </tbody>
template< class T > [[noreturn]] void throw_with_nested( T&& t );
( C++11)

std::decay<T>::type , std::nested_exception, std::nested_exception, , std::nested_exception, std::decay<T>::type, std::forward<T>(t). nested_exception std::current_exception, , , std::exception_ptr.

std::forward<T>(t).

, std::decay<T>::type CopyConstructible

t ,

()

.

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

//    .    ,
//     ,   
void print_exception(const std::exception& e, int level =  0)
{
    std::cerr << std::string(level, ' ') << ": " << e.what() << '\n';
    try {
        std::rethrow_if_nested(e);
    } catch(const std::exception& nestedException) {
        print_exception(nestedException, level+1);
    } catch(...) {}
}

//  ,         
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("   " + s) );
    }
}

//  ,         
void run()
{
    try {
        open_file("nonexistent.file");
    } catch(...) {
        std::throw_with_nested( std::runtime_error(" run()") );
    }
}

//        
int main()
{
    try {
        run();
    } catch(const std::exception& e) {
        print_exception(e);
    }
}

:

:  run()
 :    nonexistent.file
  : basic_ios::clear


() []
std::nested_exception
( ) []

Web Proxy Viewer  |  New URL  |  Original Page