Objective
Large and very complex programs tend to produce some hard-to-find, even unavoidable, run-time errors. When a run-time error occurs, the program cannot simply end up running, but instead falls back to the beginning of the task, indicating the error, and the user decides what to do next. The object-oriented exception handling (exception handling) mechanism is a powerful tool for the C + + language to solve this problem. When the function executes, any type of data object that is placed in the try (test) block can be thrown by a throw block and then returned along the call chain until caught by a catch block, where exception handling is performed, the exception is reported, and so on. From the throw to the capture, each nested call function should be removed from the stack of automatic objects, automatic variables, and field-protected content. The entry function is terminated by abort () if the Exit function has not yet been captured.
The exception (exception) described here is what the program might detect, run-time anomalies, such as storage space exhaustion, array out-of-bounds, being 0, and so on, to anticipate where it might occur, but not exactly how and when it happened. Especially in a large-scale program (software), the parts of the program are written by different groups, which are connected by public interfaces, errors may occur in mutual coordination, and may occur in a combination of individual conditions that could not have been previously imagined.
C + + provides some built-in language features to produce (raise) or throw (throw) exceptions to notify "Exceptions have occurred," and then to catch (catch) exceptions by pre-arranged fields and process them. This mechanism allows for "exception" communication in two unrelated (often independently developed) parts of a C + + program. Another part of the exception is thrown by one part of the program, which can go back to the part that caused the exception to be processed (along the call chain of the program function). It is also a good way to distinguish between responsibilities.
Exception specification
The C + + function has 2 return values, the left side is normal return, and the right side is the exception return
float Div (int a,int b) default is to throw all exceptions
The float Div (int a,int b) throw (int,short,test) specifies that only exceptions of type int, short, Test can be thrown. If it is a float type exception, it cannot be thrown
Mechanism of exception handling
First, exceptions are often implemented in C + + with classes (Class), and in the case of stacks, the exception class is declared as follows:
Class poponempty{...}; Stack NULL exception
Class pushonfull{...}; Stack Full exception
c++--Exception Handling