2. c ++ language Exception Handling
2.1 C ++ Exception Handling syntax
Thanks to the later programmers of the C ++ language, they have integrated the exception handling syntax in the Standard C ++ language (different from that, all C standard library exception systems require the support of the Runtime Library, which is not supported by the Language kernel ). Of course, the addition of exception handling to programming languages is also an inevitable result of the development and gradual improvement of programming languages. We can see that C ++ is not the only language that integrates exception handling.
The exception handling structure of C ++ is:
Try { // Code that may cause exceptions } Catch (type_1 E) { // Handle type_1 type exceptions } Catch (type_2 E) { // Handle type_2 type exceptions } Catch (...) // catch all exceptions that have not been captured and must last { } |
Exception throws use throw (Type E). Try, catch, and throw are all keywords added by C ++ to handle exceptions. Let's take a look at this example:
# Include <stdio. h> // Define the point struct (class) Typedef struct tagpoint { Int X; Int y; } Point; // Throw the int exception Function Static void F (int n) { Throw 1; }// Throw a function with a point exception Static void F (point) { Point P; P. x = 0; P. Y = 0; Throw P; } Int main () { Point point; Point. x = 0; Point. Y = 0; Try { F (point); // throw a point exception // F (1); // throw an int exception } Catch (int e) { Printf ("caught int exception: % d \ n", e ); } Catch (point E) { Printf ("point caught exception :( % d, % d) \ n", E. X, E. y ); } Return 0; } |
Function f defines two versions: F (INT) and F (point), throwing int and point exceptions respectively. When the main function try {...} When calling F (point) and F (1), the output is respectively:
Point exception caught: (0, 0)
And
Int exception caught: 1
In C ++, throw throws exceptions with the following features:
(1) can throw basic data type exceptions, such as int and char;
(2) You can throw a complex data type exception, such as struct (in C ++, struct is also a class) and class;
(3) The caller must take the initiative to handle C ++ exceptions. Once an exception is thrown and the program is not captured, the abort () function is called. The dialog box shown in 1 is displayed and the program is terminated;
(4) You can add throw ([type-ID-list]) after the function header to give an exception specification and declare the types of exceptions that can be thrown. Type-ID-list is an option that includes one or more types of names separated by commas. If no exception type is specified for a function, an exception of any type can be thrown.
2.2 standard exceptions
Some standard exceptions provided by C ++ are given below:
Namespace std { // Exception Derivation Class logic_error; // logical error, which can be detected before running the program.// Logic_error is derived. Class domain_error; // The precondition is violated. Class invalid_argument; // specifies an invalid parameter of the function. Class length_error; // an attempt to point out an object that exceeds the maximum value of the present value of the size_t type Class out_of_range; // parameter out of bounds Class bad_cast; // an invalid dynamic_cast expression exists in the runtime type recognition. Class bad_typeid; // report has a null pointer P in expression test typeid (* P) // Exception Derivation Class runtime_error; // runtime error, detected only during program running // Runtime_error Derivation Class range_error; // violates the post Condition Class overflow_error; // report an arithmetic Overflow Class bad_alloc; // Storage Allocation Error } |
Observe the hierarchy of the above classes. We can see that all standard exceptions are derived from a common base class exception. The base class contains necessary polymorphism functions that provide exception descriptions and can be overloaded. The following is the prototype of the exception class:
Class exception { Public: Exception () Throw (); Exception (const exception & RHs) Throw (); Exception & operator = (const exception & RHs) Throw (); Virtual ~ Exception () Throw (); Virtual const char * What () const throw (); }; |
One of the important functions is what (), which returns a string pointer indicating an exception. Next we will derive a class from the exception class:
# Include <iostream> # Include <exception> Using namespace STD;Class myexception: Public exception { Public: Myexception (): exception ("Example of an overloaded exception ") {} }; Int main () { Try { Throw myexception (); } Catch (exception & R) // catch an exception { Cout <"caught exception:" <R. What () <Endl; } Return 0; } |
Program running and output:
Caught exception: An Example of overloading exception
In general, we directly capture exceptions using the base class. For example, we use
Then, the processing is performed based on the polymorphism of the base class, because what function in the base class is a virtual function.