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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

exception ----- Functions

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

/* current_exception */

exception_ptr current_exception() noexcept;

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

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

?

/* get_terminate */

terminate_handler get_terminate() noexcept;

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

?

/* get_unexpected */

unexpected_handler get_unexpected() noexcept;

當(dāng)函數(shù)拋出throw列表中未聲明的異常類型時(shí),系統(tǒng)自動(dòng)調(diào)用unexpected處理函數(shù)。如果未指定,那么該函數(shù)將返回一個(gè)unspecified value。

?

/* make_exception_ptr */

template <class E>

exception_ptr make_exception_ptr(E e) noexcept;

返回一個(gè)指向e的副本的exception_ptr對(duì)象。其行為等價(jià)于如下:

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所指的異常對(duì)象。此時(shí)參數(shù)p不能為null exception_ptr,否則將引起未定義的行為。

?

/* set_terminate */

terminate_handler set_terminate(terminate_handler f) noexcept;

將f設(shè)置為終止處理函數(shù)。如果沒(méi)有調(diào)用該函數(shù)設(shè)置f,那么系統(tǒng)在適當(dāng)時(shí)候會(huì)調(diào)用abort()。程序中可以通過(guò)調(diào)用terminate()來(lái)顯式調(diào)用當(dāng)前的終止處理函數(shù),即顯式調(diào)用f或者abort()。

該函數(shù)不拋出異常,如果f是無(wú)效的或者沒(méi)有被正確的實(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(),但是也可以通過(guò)set_terminate()函數(shù)來(lái)指定。

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)拋出了異常,但是還沒(méi)有被合適的catch語(yǔ)句塊處理,則返回true,否則返回false。

?

/* unexpected */

[[noreturn]] void unexpected();

調(diào)用當(dāng)前unexpected處理函數(shù)。默認(rèn)情況下調(diào)用terminate()。但是可以通過(guò)set_unexpected()來(lái)指定。

?

/* throw_with_nested */

[[noreturn]] template <class T>

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

拋出一個(gè)聯(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的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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