C++ Exception

来源:互联网 发布:linux usr local bin 编辑:程序博客网 时间:2024/06/10 19:01

Exception type:

Derived types (scattered throughout different library headers)


Indirectly (through logic_error):

Indirectly (through runtime_error):

Multiple handlers (i.e., catch expressions) can be chained; each one with a different parameter type. Only the handler whose argument type matches the type of the exception specified in the throw statement is executed.

If an ellipsis (...) is used as the parameter of catch, that handler will catch any exception no matter what the type of the exception thrown. This can be used as a default handler that catches all exceptions not caught by other handlers:

123456
try {  // code here}catch (int param) { cout << "int exception"; }catch (char param) { cout << "char exception"; }catch (...) { cout << "default exception"; }
 


In this case, the last handler would catch any exception thrown of a type that is neither int nor char.

After an exception has been handled the program, execution resumes after the try-catch block, not after the throwstatement!.

It is also possible to nest try-catch blocks within more external try blocks. In these cases, we have the possibility that an internal catch block forwards the exception to its external level. This is done with the expression throw; with no arguments. For example: 

1234567891011
try {  try {      // code here  }  catch (int n) {      throw;  }}catch (...) {  cout << "Exception occurred";}

Exception specification

Older code may contain dynamic exception specifications. They are now deprecated in C++, but still supported. A dynamic exception specification follows the declaration of a function, appending a throw specifier to it. For example:

 
double myfunction (char param) throw (int);
 


This declares a function called myfunction, which takes one argument of type char and returns a value of type double. If this function throws an exception of some type other than int, the function calls std::unexpected instead of looking for a handler or calling std::terminate.

If this throw specifier is left empty with no type, this means that std::unexpected is called for any exception. Functions with no throw specifier (regular functions) never call std::unexpected, but follow the normal path of looking for their exception handler.

12
int myfunction (int param) throw(); // all exceptions call unexpectedint myfunction (int param);         // normal exception handling 

void unexpected();
Function handling unexpected exceptions
Calls the current unexpected handler.

By default, the unexpected handler calls terminate. But this behavior can be redefined by calling set_unexpected.

This function is automatically called when a function throws an exception that is not listed in its dynamic-exception-specifier (i.e., in its throw specifier).

This function is provided so that the unexpected handler can be explicitly called by a program, and works even ifset_unexpected has not been used to set a custom unexpected handler (calling terminate in this case).



0 0