exception ----- Functions
/* current_exception */
exception_ptr current_exception() noexcept;
返回指向當(dāng)前異常(或其副本)的智能指針【具體返回對象本身還是副本,是由具體實(shí)現(xiàn)庫決定的】,如果當(dāng)前沒有異常發(fā)生,那么返回一個null-pointer。exception_ptr是一種shared smart pointer類型:只要仍然有一個exception_ptr指向它,那么被指向的exception對象必須保持有效狀態(tài),因此,可以利用exception_ptr跨線程間處理異常。
current_exception函數(shù)不拋出異常,但是如果實(shí)現(xiàn)該函數(shù)時,返回的是指向當(dāng)前副本的指針,那么如果分配內(nèi)存失敗或者復(fù)制副本過程失敗,將返回一個bad_exception或者一些未定義的值。
?
/* get_terminate */
terminate_handler get_terminate() noexcept;
返回終止處理函數(shù)。當(dāng)沒有catch語句塊可以匹配時,自動調(diào)用該函數(shù)終止程序的執(zhí)行。如果之前系統(tǒng)中沒有通過set_terminate函數(shù)設(shè)置終止處理函數(shù),那么根據(jù)不同實(shí)現(xiàn)系統(tǒng)可能返回一個abort()或者null-pointer。
?
/* get_unexpected */
unexpected_handler get_unexpected() noexcept;
當(dāng)函數(shù)拋出throw列表中未聲明的異常類型時,系統(tǒng)自動調(diào)用unexpected處理函數(shù)。如果未指定,那么該函數(shù)將返回一個unspecified value。
?
/* make_exception_ptr */
template <class E>
exception_ptr make_exception_ptr(E e) noexcept;
返回一個指向e的副本的exception_ptr對象。其行為等價于如下:
template <class E> exception_ptr make_exception_ptr (E e) noexcept {
? try {
???? throw e;
? } catch(...) {
???? return current_exception();
? }
}
1 // make_exception_ptr example 2 #include <iostream> // std::cout 3 #include <exception> // std::make_exception_ptr, std::rethrow_exception 4 #include <stdexcept> // std::logic_error 5 6 int main() 7 { 8 auto p = std::make_exception_ptr(std::logic_error("logic_error")); 9 10 try 11 { 12 std::rethrow_exception (p); 13 } 14 catch(const std::exception& e) 15 { 16 std::cout << "exception caught: " << e.what() << '\n'; 17 } 18 19 return 0; 20 }?
/* rethrow_exception */
[[noreturn]] void rethrow_exception(exception_ptr p);
拋出p所指的異常對象。此時參數(shù)p不能為null exception_ptr,否則將引起未定義的行為。
?
/* set_terminate */
terminate_handler set_terminate(terminate_handler f) noexcept;
將f設(shè)置為終止處理函數(shù)。如果沒有調(diào)用該函數(shù)設(shè)置f,那么系統(tǒng)在適當(dāng)時候會調(diào)用abort()。程序中可以通過調(diào)用terminate()來顯式調(diào)用當(dāng)前的終止處理函數(shù),即顯式調(diào)用f或者abort()。
該函數(shù)不拋出異常,如果f是無效的或者沒有被正確的實(shí)現(xiàn),那么將引起未定義的行為。
1 // set_terminate example 2 #include <iostream> // std::cerr 3 #include <exception> // std::set_terminate 4 #include <cstdlib> // std::abort 5 6 void myterminate() 7 { 8 std::cerr << "terminate handler called\n"; 9 abort(); // forces abnormal termination 10 } 11 12 int main() 13 { 14 std::set_terminate(myterminate); 15 throw 0; // unhandled exception: calls terminate handler 16 17 return 0; 18 }?
/* set_unexpected */
unexpected_handler set_unexpected(unexpected_handler f) noexcept;
?
/* terminate */
[[noreturn]]void terminate() noexcept;
調(diào)用當(dāng)前終止處理函數(shù)。默認(rèn)情況下調(diào)用abort(),但是也可以通過set_terminate()函數(shù)來指定。
1 // terminate example 2 #include <iostream> // std::cout, std::cerr 3 #include <exception> // std::exception, std::terminate 4 5 int main() 6 { 7 char* p, *p2; 8 std::cout << "Attempting to allocate 2 GB at the same point ..."; 9 try 10 { 11 p = new char[1024*1024*1024]; 12 p2 = new char[1024*1024*1024]; 13 } 14 catch (std::exception& e) 15 { 16 std::cerr << "ERROR: could not allocate storage\n"; 17 std::terminate(); 18 } 19 std::cout << "Ok\n"; 20 21 delete[] p2; 22 delete[] p; 23 return 0; 24 }?
/* uncaught_exception */
bool uncaught_exception() noexcept;
如果已經(jīng)拋出了異常,但是還沒有被合適的catch語句塊處理,則返回true,否則返回false。
?
/* unexpected */
[[noreturn]] void unexpected();
調(diào)用當(dāng)前unexpected處理函數(shù)。默認(rèn)情況下調(diào)用terminate()。但是可以通過set_unexpected()來指定。
?
/* throw_with_nested */
[[noreturn]] template <class T>
??? void throw_with_nested(T&& e);
拋出一個聯(lián)合了當(dāng)前異常及指定e的嵌套異常。當(dāng)前異常變?yōu)閚ested exception,而指定e變?yōu)閛uter exception。
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/benxintuzi/p/4617984.html
總結(jié)
以上是生活随笔為你收集整理的exception ----- Functions的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++ 哪些自定义的数据类型
- 下一篇: 科研经验:一篇学术文章要写多久