日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

exception ----- Functions

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

/* current_exception */

exception_ptr current_exception() noexcept;

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

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

?

/* get_terminate */

terminate_handler get_terminate() noexcept;

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

?

/* get_unexpected */

unexpected_handler get_unexpected() noexcept;

當函數(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ù)。如果沒有調(diào)用該函數(shù)設置f,那么系統(tǒng)在適當時候會調(diào)用abort()。程序中可以通過調(diào)用terminate()來顯式調(diào)用當前的終止處理函數(shù),即顯式調(diào)用f或者abort()。

該函數(shù)不拋出異常,如果f是無效的或者沒有被正確的實現(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)用當前終止處理函數(shù)。默認情況下調(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)用當前unexpected處理函數(shù)。默認情況下調(diào)用terminate()。但是可以通過set_unexpected()來指定。

?

/* throw_with_nested */

[[noreturn]] template <class T>

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

拋出一個聯(lián)合了當前異常及指定e的嵌套異常。當前異常變?yōu)閚ested exception,而指定e變?yōu)閛uter exception。

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/benxintuzi/p/4617984.html

總結

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。