1 Introduction
Exceptions are C ++ language standard library itself or thrown exception subclass called standard exceptions. Catch all standard exceptions statement:
the try { // may throw the exception } the catch (Exception & E) // references are used in order to improve efficiency, or to go through a process of copying the object. { // exception handling statements }
1.1 exception class
class located exception <exception> header file, is declared as:
class Exception { public : Exception () the throw (); // Constructor Exception ( const Exception &) the throw (); // copy constructor Exception & operator = ( const Exception &) the throw (); // operator overloading Virtual ~ Exception () the throw (); // virtual destructor virtual const char * What () const the throw (); // virtual function }
what function returns a string that can identify anomalies, the programmer can tell this is anything unusual, but the C ++ standard does not specify the format of the string to achieve the various compiler is different, so what's the return value for reference only.
1.2 exception class inheritance hierarchy
1.2.1 exception of direct derived class
The exception name |
Description |
---|---|
logic_error |
logical error. |
runtime_error |
Run-time error. |
bad_alloc |
The use of new or new [] throws an exception when memory allocation fails. |
bad_typeid |
Typeid operation using a NULL pointer, and the pointer is a class with a virtual function, then bad_typeid thrown exception. |
bad_cast |
Use dynamic_cast conversion failed when thrown exception. |
ios_base::failure |
Io anomaly occurred during. |
bad_exception |
This is a special exception, if the function's exception list declares bad_exception exception when internal function throws an exception list is no exception, if you call the unexpected () function throws an exception, regardless of what type, will be replace bad_exception type. |
1.2.2 logic_error derived class
The exception name |
Description |
---|---|
length_error |
Try to build out of the anomaly of an object exceeds the maximum length of this type, for example, the vector resize operation. |
domain_error |
Range parameter errors, mainly used in mathematical functions, such as using a negative function call can only operate non-negative. |
out_of_range |
It is out of range. |
invalid_argument |
Parameters inappropriate. In the standard library, when using the string object construction bitset, and the string of characters is not the time to 0 or 1, Thrown. |
1.2.3 runtime_error derived class
The exception name |
Description |
---|---|
range_error |
The results exceeded the significant value range. |
overflow_error |
Arithmetic overflow. |
underflow_error |
Arithmetic underflow. |