To write an appropriate catch clause, it is helpful for function users to know whether a function throws an exception or what exceptions will be thrown.
The exception description can be used to describe the exception of a function. If a function throws an exception, the thrown exception is either included in this description or derived from the listed exception.
Exceptions are described as follows:
1. Specify an exception
T funNname (parameterlist) throw (T1, T2,..., Tn );
T is the type, parameterlist is the parameter list, and Type T1, T2,..., Tn is an exception thrown by the function.
2. Do not throw an exception
T funNname (parameterlist) throw ();
The list of thrown exception types is empty, indicating that this function does not throw any type exceptions.
3. Throw any type of exception
T funNname (parameterlist );
This indicates that the function can throw any type of exception.
The following code describes the exceptions.
[Cpp]
# Include <iostream>
Class demo
{
};
Using namespace std;
Double divd (int a, int B) throw (int) // exception description, indicating that the divd function will throw an exception of the int type.
{
If (B = 0) throw demo (); // throw an exception of the demo type
Return a/B;
}
Int main ()
{
Try
{
Divd (1, 0 );
}
Catch (demo) // catch exception type demo
{
Cout <"divided by zero" <endl;
}
Catch (int) // catch the exception type int
{
Cout <"zero" <endl;
}
Return 0;
}
The output of this Code is divided by zero.
It is strange that in the divd function declaration, only the int type exception is thrown. Why is the exception type thrown in the function demo?
We can temporarily understand that: when the types listed in the exception description of a function do not exactly match the exception types thrown in the function, however, the catch in the exception handling code captures its type, so the program runs normally.
Okay. Let's make some modifications based on the exception description.
[Cpp]
Double divd (int a, int B) throw () // change throw (int) to throw ()
After the modification, compile and run the program. The output result is: divided by zero.
According to the three methods described above, we know that adding throw () after the function declaration means that this function will not throw any exceptions. Why is it thrown and captured?
In fact, during compilation, the compiler cannot and will not try to verify the exception description. Because the exception description cannot be checked during compilation, the application of the exception description is usually limited!
The modified divd function continues to write code that throws an exception. After compilation, the program runs again. The output result also indicates that the exception mechanism runs normally.
The reason is as mentioned above: the compiler does not detect the exception description, and the exception description is more written to the function users.
In the case that the type of the function exception description does not match the exception type actually thrown at the beginning, the compilation passes because the compiler does not detect the exception description, the exception mechanism runs normally.
Summary: exceptions are useful! But more is written to function users. Let the function user clearly know the thrown exception type, so as to better and correctly use this function, write the exception detection.
When learning the syntax, beginners only read books and do not perform practical verification. Even if it has been verified by practice, you may just write the code in the book to check whether the running results are the same. Here is a suggestion: when learning the syntax, Beginners should practice more on the computer to verify the accuracy of the syntax in the book. Even if the running result is the same as that in the book, they should also use their brains, deformation or modification. In this way, you will learn more and get a deeper impression!