日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

exception ----- Functions

發布時間:2025/4/5 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 exception ----- Functions 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/* current_exception */

exception_ptr current_exception() noexcept;

返回指向當前異常(或其副本)的智能指針【具體返回對象本身還是副本,是由具體實現庫決定的】,如果當前沒有異常發生,那么返回一個null-pointer。exception_ptr是一種shared smart pointer類型:只要仍然有一個exception_ptr指向它,那么被指向的exception對象必須保持有效狀態,因此,可以利用exception_ptr跨線程間處理異常。

current_exception函數不拋出異常,但是如果實現該函數時,返回的是指向當前副本的指針,那么如果分配內存失敗或者復制副本過程失敗,將返回一個bad_exception或者一些未定義的值。

?

/* get_terminate */

terminate_handler get_terminate() noexcept;

返回終止處理函數。當沒有catch語句塊可以匹配時,自動調用該函數終止程序的執行。如果之前系統中沒有通過set_terminate函數設置終止處理函數,那么根據不同實現系統可能返回一個abort()或者null-pointer。

?

/* get_unexpected */

unexpected_handler get_unexpected() noexcept;

當函數拋出throw列表中未聲明的異常類型時,系統自動調用unexpected處理函數。如果未指定,那么該函數將返回一個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所指的異常對象。此時參數p不能為null exception_ptr,否則將引起未定義的行為。

?

/* set_terminate */

terminate_handler set_terminate(terminate_handler f) noexcept;

將f設置為終止處理函數。如果沒有調用該函數設置f,那么系統在適當時候會調用abort()。程序中可以通過調用terminate()來顯式調用當前的終止處理函數,即顯式調用f或者abort()。

該函數不拋出異常,如果f是無效的或者沒有被正確的實現,那么將引起未定義的行為。

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;

調用當前終止處理函數。默認情況下調用abort(),但是也可以通過set_terminate()函數來指定。

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;

如果已經拋出了異常,但是還沒有被合適的catch語句塊處理,則返回true,否則返回false。

?

/* unexpected */

[[noreturn]] void unexpected();

調用當前unexpected處理函數。默認情況下調用terminate()。但是可以通過set_unexpected()來指定。

?

/* throw_with_nested */

[[noreturn]] template <class T>

??? void throw_with_nested(T&& e);

拋出一個聯合了當前異常及指定e的嵌套異常。當前異常變為nested exception,而指定e變為outer exception。

?

?

轉載于:https://www.cnblogs.com/benxintuzi/p/4617984.html

總結

以上是生活随笔為你收集整理的exception ----- Functions的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。