c ++ throw an exception is thrown

 Exception is thrown (also referred to discard abnormality) detecting whether an abnormality i.e., in C ++ , which is employed throw statement to achieve, if an abnormality is detected, an exception is thrown. The statement of the form:
the throw expression;
    the block if the try block (including a function which calls) found abnormal, the abnormality and discarded, then the exception may be a try block catch statement captured and processed, the processing conditions are captured and the exception type and the type of exception catch clause is discarded match. Because C ++ uses the data to distinguish between different types of exceptions, so in judging abnormal, the throw value of the expression in the statement there is no practical significance, and the type of expression is particularly important.
[20-2] Examples of abnormal divisor zero. The above example division by zero anomalies can try / catch statement to catch exceptions, and use throw statement to throw an exception, in order to achieve exception processing, such as inventory codes codes 20-2.
Listing 20-2
. 1 #include <, iostream.h> // include file
2 #include <stdlib.h>
. 3 Double Fuc (Double X, Y Double) // defined function
. 4 {
. 5 IF (Y == 0)
{. 6
. 7             the throw Y; // divide by zero, an exception is thrown
. 8}
. 9 return X / Y; // otherwise quotient of two numbers
10}
. 11 void main ()
12 is {
13 is Double RES;
14 // definition of the try exception
15 {
16 Fuc RES = (2,3);
. 17 COUT << "Result of of The X / Y IS:" << endl << RES;
18 is Fuc RES = (4,0); abnormal //
19}
20 catch (double) // capture and exception processing
21 {
22 is cerr, <<. "Error of dividing Is ZERO \ n-";
23 is Exit (. 1); // abnormal exit the program
24}
25}
[Run Results] In the Visual C ++ new a [in C ++ the Source File] file, input to the code run compiled correctly.
[Example A] In the above code, line 19 - 14 in the primary main () function used in the try statement an exception, which comprises the statement has three possible abnormality, which is divided by two logarithmic function call. At the 20-24 line of the definition of the exception handling, i.e. capturing the segment code statement execution after the exception. Further, the function Fuc () code by lines 5-8 throw throwing an exception statement.

Reproduced in: https: //www.cnblogs.com/yuhua4/archive/2009/10/15/1584095.html

Guess you like

Origin blog.csdn.net/weixin_34221112/article/details/93699812