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

std::nested_exception cppreference.com
cppreference.com

std::nested_exception

cppreference.com
<tbody> </tbody>
class nested_exception;
( C++11)

std::nested_exception , , .

-

nested_exception
(public -)
[virtual]
nested_exception
(virtual public -)
nested_exception
(public -)

(public -)

(public -)

,

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

.

#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
( ) []
std::nested_exception
( ) []

Web Proxy Viewer  |  New URL  |  Original Page