C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- std::exception_ptr
- std::error_code
- std::error_condition
- std::terminate
- std::exception
- std::current_exception
- std::rethrow_exception
- std::make_exception_ptr
- std::unexpected
- std::uncaught_exception, std::uncaught_exceptions
- assert
- errno
- std::nested_exception
- std::throw_with_nested
- std::rethrow_if_nested
- std::terminate_handler
- std::get_terminate
- std::set_terminate
- std::bad_exception
- std::unexpected_handler
- std::get_unexpected
- std::set_unexpected
- é误å·
- std::logic_error
- std::invalid_argument
- std::domain_error
- std::length_error
- std::out_of_range
- std::runtime_error
- std::range_error
- std::overflow_error
- std::underflow_error
- std::tx_exception
- std::error_category
- std::generic_category
- std::system_category
- std::errc
- std::system_error
- 注é
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >é误å¤ç > std::rethrow_if_nested
std::rethrow_if_nested
| å®ä¹äºå¤´æä»¶ <exception>
|
||
| template< class E > void rethrow_if_nested( const E& e ); |
(C++11 èµ·) | |
è¥ E 䏿¯å¤æç±»ç±»åï¼æè¥ std::nested_exception æ¯ E çä¸å¯è®¿é®æææ§ä¹åºç±»ï¼åæ ææã
å¦åï¼è¿è¡
if (auto p = dynamic_cast<const std::nested_exception*>(std::addressof(e))) p->rethrow_nested();
åæ°
| e | - | è¦éæçå¼å¸¸å¯¹è±¡ |
è¿åå¼
ï¼æ ï¼
å¯è½çå®ç°
namespace details { template <class E> struct can_dynamic_cast : std::integral_constant<bool, std::is_polymorphic<E>::value && (!std::is_base_of<std::nested_exception, E>::value || std::is_convertible<E*, std::nested_exception*>::value) > { }; Â template <class T> void rethrow_if_nested_impl(const T& e, std::true_type) { if(auto nep = dynamic_cast<const std::nested_exception*>(std::addressof(e))) nep->rethrow_nested(); } Â template <class T> void rethrow_if_nested_impl(const T&, std::false_type) { } } Â template <class T> void rethrow_if_nested(const T& t){ details::rethrow_if_nested_impl(t, details::can_dynamic_cast<T>()); } |
示ä¾
æ¼ç¤ºæé å¹¶éè¿ 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, ' ') << "exception: " << e.what() << '\n'; try { std::rethrow_if_nested(e); } catch(const std::exception& e) { print_exception(e, level+1); } catch(...) {} }  // 示ä¾å½æ°ï¼ææå¼å¸¸å¹¶å°å ¶å è£ äº 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) ); } }  // 示ä¾å½æ°ï¼ææå¼å¸¸å¹¶å°å ¶å è£ äº nested_exception void run() { try { open_file("nonexistent.file"); } catch(...) { std::throw_with_nested( std::runtime_error("run() failed") ); } }  // è¿è¡ä¸è¿°å®ä¾å½æ°å¹¶æå°ææçå¼å¸¸ int main() { try { run(); } catch(const std::exception& e) { print_exception(e); } }
è¾åºï¼
exception: run() failed exception: Couldn't open nonexistent.file exception: basic_ios::clear
åé
| (C++11) |
ç¨äºæè·å¹¶åå¨å½åå¼å¸¸çæ··å
¥ç±»å (ç±») |
| (C++11) |
æåºå®åï¼å¸¦ä¸æ··å
¥ç std::nested_exception (彿°æ¨¡æ¿) |