A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/exception/nested_exception/ below:

class

<exception>

std::nested_exception

Nested exception class

Component for exception classes that captures the currently handled exception as nested.

A class that derives from both this class and some other exception class, that can hold the properties of both the currently handled exception (as its nested exception) and this other exception (its outer exception).

Objects with nested exceptions are generally constructed by calling throw_with_nested with the outer exception object as argument. The returned object has the same properties and members as the outer exception, but carry additional information related to the nested exception and includes two member functions to access this nested exception: nested_ptr and rethrow_nested.

It is declared as:


1
2
3
4
5
6
7
8
9
10
class nested_exception {
public:
  nested_exception() noexcept;
  nested_exception (const nested_exception&) noexcept = default;
  nested_exception& operator= (const nested_exception&) noexcept = default;
  virtual ~nested_exception() = default;

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

Member functions
(constructor)
Construct nested_exception (public member function)
rethrow_nested
Rethrow nested exception (public member function)
nested_ptr
Get exception_ptr to nested exception (function)

The copy assignment and the virtual destructor are explicitly defaulted.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// nested_exception example
#include <iostream>       // std::cerr
#include <exception>      // std::exception, std::throw_with_nested, std::rethrow_if_nested
#include <stdexcept>      // std::logic_error

// recursively print exception whats:
void print_what (const std::exception& e) {
  std::cerr << e.what() << '\n';
  try {
    std::rethrow_if_nested(e);
  } catch (const std::exception& nested) {
    std::cerr << "nested: ";
    print_what(nested);
  }
}

// throws an exception nested in another:
void throw_nested() {
  try {
    throw std::logic_error ("first");
  } catch (const std::exception& e) {
    std::throw_with_nested(std::logic_error("second"));
  }
}

int main () {
  try {
    throw_nested();
  } catch (std::exception& e) {
    print_what(e);
  }

  return 0;
}

Output:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4