directory
-
- C++ exception handling knowledge points
- Kick a sample code block to catch
- Two ways to handle division by 0
- Advantages of exception handling mechanisms
- Exception handling in other languages
- C++ function exception declaration
C++ exception handling knowledge points
Understand the structure of the Kick, Throw and Catch trilogy, especially how catch matches exceptions.
Know that exception classes in the C++ standard library inherit from exception and support what()
Learn about logic_error, runtime_error, and other common exception classes so that we can inherit them when we write them
Understand the rules for matching multiple catch statements, especially for exception types on an inheritance chain, and know which comes first and which comes last
Learn how exception propagation works: If an exception is not caught in the current function, it will be thrown to the caller of the current function. Any statement following an exception is skipped until a catch is encountered
Kick a sample code block to catch
try{
code to try;
throw an exception (1)with a throw statement
(2) or from function
More code to try;
}
catch(type e){
Code to process the exception;
}
Copy the code
Two ways to handle division by 0
(1) Use if statement (2) Use exception handling
Here is a program that uses exception handling, throwing number1 if the divisor is 0; After the throw statement is executed, the cout statement immediately following it is skipped; If you can catch number1, it will be processed and no program error will occur. If the Type does not match, it will not be caught and the program will crash.
int main(a) {
// Read two intergers
cout << "Enter two integers: ";
int number1, number2;
cin >> number1 >> number2;
try {
if (number2 == 0)
throw number1;
cout << number1 << "/" << number2 <<
" is " << (number1 / number2) << endl;
}
catch (int e) {
cout << "Exception: an integer " << e <<
" cannot be divided by zero" << endl;
}
cout << "Execution continues ..." << endl;
}
Copy the code
Advantages of exception handling mechanisms
Exception information can be brought back from the called function to the calling function. Exception handling is really control over the flow of program execution
An exception is thrown out of the test instead of being handled. The problem is handled by the main function. To separate exception discovery from handling.
// Use exception handling
int quotient(int number1,
int number2) {
if (number2 == 0)
throw number1;
return number1 / number2;
}
int main(a) {
try {
int x = quotient(1.0);
} catch (int) {
std::cout << "The divisor is 0!"; }}Copy the code
How does Quotient () tell Main () that “number2 has a problem”? We can think of it in terms of return values and references, but if a function is called in a nested way, how do we get exception information back to the calling function
(1) Use the return value? ·f(number2 == 0) return x; // should x be 0 or 1? (2) Use pointer/reference type parameters? Then the main function looks at this parameter, but it’s not elegant. int quotient(int n1, int n2, int &s){ if(n2 == 0) s=-1; // The function takes 3 arguments. } (3) If f(g(h(x,y)))); How to pass an error from Quotient () to F ()? If (2) is used, do you want to add a reference type argument to each function so that the calling function can check for exceptions? Obviously, exception handling is much cleaner in this case
Exception handling in other languages
Python:
tryCode:raiseXXX throws an exceptionexcept: The command is executed if an error occurselse: Executes when no error occursfinallyDo it anywayCopy the code
On Java and C++ : in C++, you can throw any type of exception. Java function exception declarations are checked at compile time, so if it is any other Java function that calls ABC, the Java virtual machine forces other functions that call ABC to handle matherr, error and other exceptions. When C++ is compiled, other functions that declare these calls to throw exceptions do not force them to handle exceptions that might be thrown.
C++ function exception declaration
1. Declare exceptions that may be thrown after a function argument list: C++ has always followed a function with a “:” + throw+ “()”, enclosing one or more exception classes in parentheses, separated by commas. Void ABC (int a) : throw (matherr, error) {… } This tells other programs the maximum number of exception classes in parentheses that can be thrown at the time the ABC function runs. 2. An exception that may be thrown at the time the function is running is guaranteed not to be thrown except for matherr and error in parentheses.
C++ function exception declarations are not used to constrain calls to other functions whose declarations may throw the exception ABC function. Any other function call ABC must handle matherr, error and other exceptions. It is used to constrain the behavior of the ABC function itself. The ABC function declaration will raise matherr error. If the ABC function runtime throws an exception other than Matherr error, unexpected exceptions will be thrown and the program will terminate. ·C++ functions declare/not declare exceptions:
·1, void ABC (int a) {… } // Ordinary functions
A normal function means that any exception can be thrown
·2, void ABC (int a):throw() {… } // A function that does not throw exceptions
Throw () after the function argument, with nothing in parentheses, to indicate that the function promises not to throw any exceptions!
·3, void ABC (int a):throw(matherr) {… } // The function throws matherr
The function argument is followed by throw(), with matherr in parentheses, indicating that there is a mechanism at runtime to verify that the function is true and only throws the matherr exception in the list.
· Different from Java functions that declare/do not declare exceptions:
·1, void ABC (int a) {… } // Ordinary functions
A normal function means that no exception is thrown
·2, void ABC (int a) throw(exception) {… } // throw() after any exception function arguments, with exception in parentheses, indicating that this function will throw any exceptions!