exception -----> Functions

來源:互聯網
上載者:User

標籤:

/* 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 termination10 }11 12 int main()13 {14   std::set_terminate(myterminate);15   throw 0;  // unhandled exception: calls terminate handler16 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     try10     {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。

 

 

exception -----> Functions

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.