exception -----> Typedefs & Classes

#include <exception>ios

 

Typedefsdom

exception_ptr函數

一種類型,描述了一個指向異常的指針ui

terminate_handlerspa

一種類型,描述了一個適合做爲terminate_handler的函數的指針線程

unexperted_handler3d

一種類型,描述了一個適合做爲unexpected_handler的函數的指針指針

 

Functionscode

current_exception對象

得到當前異常的指針

get_terminate

得到當前terminate_handler函數

get_unexpected

得到當前unexpected_handler函數

make_exception_ptr

建立一個包含exception副本的exception_ptr對象

rethrow_exception

拋出一個以參數傳遞的異常

set_terminate

創建一個新的terminate_handler,以便在程序結束時調用

set_unexpected

創建一個新的unexpected_handler,以便在程序遇到未知類型異常時調用

terminate

調用terminate handler

uncaught_exception

若是當前正在處理一個已經拋出的異常,那麼返回ture

unexpected

調用一個未知的處理程序

 

Classes

bad_exception Class

描述一種異常,該異常可能從unexpected_handler中拋出

exception Class

全部異常類的基類

 

 

/* ************************************************************************************* */

Typedefs

/* exception_ptr */

typedef unspecified exception_ptr;

說明:

指向exception對象的智能指針類型。這是一種相似shared_ptr的類型:只要還有一個exception_ptr指向exception對象,那麼該exception對象就必須保持有效。能夠將exception_ptr對象的生命週期延伸到catch語句塊外或者不一樣線程之間

對於exception_ptr類型,不一樣的庫擁有不一樣的實現方式,可是其至少須要支持以下幾種操做:

  • 默認構造函數(接收一個null-pointer)
  • 複製構造函數(包括接收一個null-pointer或者nullptr)
  • 重載運算符operator==或者operator!=

能夠經過如下操做得到exception_ptr對象:current_exception、make_exception_ptr、nested_exception::nested_ptr;經過rethrow_exception從新拋出異常。

 1 // exception_ptr example
 2 #include <iostream>      // std::cout
 3 #include <exception>     // std::exception_ptr, std::current_exception, std::rethrow_exception
 4 #include <stdexcept>     // std::logic_error
 5 
 6 int main ()
 7 {
 8   std::exception_ptr p;
 9   try
10   {
11      throw std::logic_error("some logic_error exception"); // throws
12   }
13   catch(const std::exception& e)
14   {
15      p = std::current_exception(); 16      std::cout <<"exception caught, but continuing...\n";
17   }
18 
19   std::cout <<"(after exception)\n";
20 
21   try
22   {
23  std::rethrow_exception(p); 24   }
25   catch (const std::exception& e)
26   {
27      std::cout <<"exception caught: " << e.what() << '\n';
28   }
29 
30   return 0;
31 }
 

 

/* terminate_handler */

typedef void (*terminate_handler)();

terminate_handler是一個指向void(void)函數的指針,能夠用做set_terminate函數的參數和返回值。

 

/* unexpected_handler */

typedef void (*unexpected_handler)();

unexpected_handler是一個指向void(void)函數的指針,能夠用做set_unexpected函數的參數和返回值。

 

 

/* ************************************************************************************* */

Classes

/* exception */

class exception {

public:

  exception () noexcept;                              

  exception (const exception&) noexcept;              

  exception& operator= (const exception&) noexcept;   

  virtual ~exception();                               

  virtual const char* what() const noexcept;          

}

說明:

因此標準異常類的基類,所以exception&能夠適配全部的異常類型。

 

直接派生類:

bad_alloc

allocate memory failed

bad_cast

dynamic case failed

bad_exception

unexpected handler failed

bad_function_call

bad call

bad_typeid

typeid of null pointer

bad_weak_ptr

bad weak pointer

ios_base::failure

base class for stream exceptions

logic_error

logic error

runtime_error

runtime error

 

間接派生類:

經過logic_error派生:

domain_error

domain error

future_error

future error

invalid_argument

invalid argument

length_error

length error

out_of_range

out-of-range

經過runtime_error派生:

overflow_error

overflow error

range_error

range error

system_error

system error

underflow_error

system error

經過bad_alloc派生:

bad_array_new_length

bad array length

經過system_error派生:

ios_base::failure

base class for stream exceptions

 

示例代碼:

 1 // exception example
 2 #include <iostream>       // std::cerr
 3 #include <typeinfo>       // operator typeid
 4 #include <exception>      // std::exception
 5 
 6 class Polymorphic
 7 {
 8     virtual void member(){ }
 9 };
10 
11 int main(){
12 
13     try
14     {
15         Polymorphic * pb = 0;
16         typeid(*pb); // throws a bad_typeid exception
17     }
18     catch(std::exception& e)
19     {
20         std::cerr << "exception caught: " << e.what() << '\n';
21     }
22 
23     return 0;
24 }
 

 

/* bad_exception */

class bad_exception : public exception;

說明:

若是bad_exception在函數的throw列表中,那麼unexpected將會拋出一個bad_exception來代替terminate函數,所以調用set_unexpected指定的函數後,接着調用bad_exception的catch處理塊。

 1 // bad_exception example
 2 #include <iostream>       // std::cerr
 3 #include <exception>      // std::bad_exception, std::set_unexpected
 4 
 5 void myunexpected()
 6 {
 7     std::cerr << "unexpected handler called\n";
 8     throw;
 9 }
10 
11 void myfunction() throw(char, std::string, std::bad_exception) 12 {
13     throw 100.0;           // throws double (not in exception-specification)
14 }
15 
16 int main(void)
17 {
18  std::set_unexpected(myunexpected); 19     try
20     {
21         myfunction();
22     }
23     catch(int)
24     {
25         std::cerr << "caught int\n";
26     }
27     catch(std::bad_exception be)
28     {
29         std::cerr << "caught bad_exception: ";
30         std::cerr << be.what() << "\n";
31     }
32     catch(...)
33     {
34         std::cerr << "caught some other exception\n";
35     }
36 
37     return 0;
38 }
 

上述程序中,語句try{ myfunction(); }後並無調用terminate()函數,而是調用set_unexcepted指定的函數myunexpected(),而後調用了

catch(std::bad_exception be)

{

  std::cerr << "caught bad_exception: ";

  std::cerr << be.what() << "\n";

}

若是throw列表中沒有指定std::bad_exception,那麼在調用set_unexpected指定的函數後將會調用terminate(),以下所示:

 

 

 

 

 

 

/* nested_exception */

class nested_exception {

public:

  nested_exception() noexcept;

  nested_exception (const nested_exception&) noexcept = default;

  nested_exception& operator= (const nested_exception&) noexcept = default;

  virtual ~nested_exception() = default;

 

  [[noreturn]] void rethrow_nested() const;

  exception_ptr nested_ptr() const noexcept;

}

說明:

nested exception對象一般能夠經過throw_with_nested函數構造,只須要傳入outer exception做爲參數便可。返回的exception對象擁有與outer exception相同的屬性和成員,可是其包含了與nested exception相關的額外信息以及兩個用於訪問nested exception的成員函數:nested_ptr和rethrow_nested

 1 // nested_exception example
 2 #include <iostream>       // std::cerr
 3 #include <exception>      // std::exception, std::throw_with_nested, std::rethrow_if_nested
 4 #include <stdexcept>      // std::logic_error
 5 
 6 // recursively print exception whats:
 7 void print_what(const std::exception& e)
 8 {
 9     std::cerr << e.what() << '\n'; 10     try
11     {
12  std::rethrow_if_nested(e); 13     }
14     catch(const std::exception& nested)
15     {
16         std::cerr << "nested: "; 17         print_what(nested);
18     }
19 }
20 
21 // throws an exception nested in another:
22 void throw_nested()
23 {
24     try
25     {
26         throw std::logic_error("first"); 27     }
28     catch(const std::exception& e)
29     {
30         std::throw_with_nested(std::logic_error("second")); /* outer:second; nested:first */
31     }
32 }
33 
34 int main()
35 {
36     try
37     {
38         throw_nested();
39     }
40     catch(std::exception& e)
41     {
42         print_what(e);
43     }
44 
45     return 0;
46 }
47 
48 /** 49  output: 50  second 51  nested: first 52 */
相關文章
相關標籤/搜索