Defined:
exception, so that a function can throw an exception when it finds an error that it cannot handle, and that the caller can handle the problem directly or indirectly.
Some of the small programs that were written before have almost no use for exception handling. Because the size is small, the general problem in the function with some of the judgment conditions resolved, the general practice is to return a value that represents the error (such as returning a null pointer), when called to determine the return value, although simple, but the function is not powerful, only for small projects. and large-scale projects, if such a mess, so it is necessary to use the exception handling this system.
One of the simplest exception handling:
ConsoleApplication3.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <vector> #include <string>using namespace std;// Exception Classes Class Numberexception{};void Numoption (const int& A, const int& b) {try{if (b = = 0) throw numberexception (); cout <<a/b<<endl;} catch (const numberexception& e) {cout<< "exception!" <<endl;}} int _tmain (int argc, _tchar* argv[]) {int a,b;cin>>a;cin>>b; Numoption (A, b); system ("pause"); return 0;}
Result://input 1
0//output
exception!
There are a few things to note about exception handling: 1. After throwing an exception, the code goes to the place where the exception is handled, and the code behind the throw is no longer executed. If no related catch is found, it will go out of one level and continue looking. This process becomes the stack unfold. Therefore, it is important to note that in the process of stack expansion, the various temporary objects in the block will be destroyed. An example:
ConsoleApplication3.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <vector> #include <string>using namespace std;// Class Test{public:~test () {cout<< "test is destructed!" for exception classes class numberexception{};// <<endl;}}; void Numoption (const int& A, const int& b) {try{test test;if (b = = 0) throw numberexception (); cout<<a/b<& Lt;endl;} catch (const numberexception& e) {cout<< "exception!" <<endl;}} int _tmain (int argc, _tchar* argv[]) {int a,b;cin>>a;cin>>b; Numoption (A, b); system ("pause"); return 0;}
Normal condition://input 1
1//output
1
Test is destructed!
Exception Condition://input 1
0
OutputTest is destructed!
exception!
From the above results we can see that, under normal circumstances, the program executes, first output 1, and then destroy the test object. However, when an exception is thrown, there is obviously no scope for the block, but the exception occurs, the content behind the throw is not executed, and the declaration period ends, and all temporary objects are destroyed.
2. In general, catch blocks that accept exceptions are best set to reference types. If set to a value pass, the case where the base class parameter takes a subclass exception when it is taken is a condition in which the subclass-specific part is clipped. And we can set whether it is a const reference, and decide whether the exception can be modified in the catch block.
3.catch matching principle: The more specialized the match is forward, in other words, if there are both sub-class and base class exception handling catch, and thrown is a subclass exception, we want to be able to handle subclasses, we need to deal with class exception handling in front. Example:
ConsoleApplication3.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <vector> #include <string>using namespace std;// Exception classes class numberexception{};//derived exception classes class Zeroexception:public Numberexception {};void numoption (const int& A, Const int& b) {try{if (b = = 0) throw zeroexception (); Cout<<a/b<<endl;} catch (const zeroexception& e) {cout<< "zeroexception!" <<endl;} catch (const numberexception& e) {cout<< "numberexception!" <<endl;}} int _tmain (int argc, _tchar* argv[]) {int a,b;cin>>a;cin>>b; Numoption (A, b); system ("pause"); return 0;}
Results: 1
0//output
zeroexception!
But if we change the catch (Cost numberexception& e) and catch (const zeroexception& e) These two positions: the result becomes the numberexception!
It can be seen that in order to deal with the exception of a more special case, the more you put in front. Otherwise, the child class is no longer matched after the parent class matches.
4. Re-throw, we can continue to throw the exception when we have handled an exception but have not processed the whole time. Use throw; the keyword can continue to be thrown. However, if no corresponding catch continues to accept the exception, the program will terminate! An example of nested exception handling:
ConsoleApplication3.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <vector> #include <string>using namespace std;// Exception classes class numberexception{};//derived exception classes class Zeroexception:public Numberexception {};void numoption (const int& A, Const int& b) {try{if (b = = 0) throw zeroexception (); Cout<<a/b<<endl;} catch (const zeroexception& e) {cout<< "zeroexception!" <<endl;throw;} catch (...) {cout<< "I can solve all exception!" <<endl;}} int _tmain (int argc, _tchar* argv[]) {int a,b;cin>>a;cin>>b;//nested exception handling try{numoption (A, b);} catch (const numberexception& e) {cout<< "numberexception!" <<endl;} System ("pause"); return 0;}
Results: 1
0//output
zeroexception!
numberexception!
Throws an exception, first through the zeroexception exception handling, after processing continues to throw, outside the numberexception receives.
5. Use catch (...) You can catch all the exceptions:
catch (...) {cout<< "I can solve all exceptions!" <<endl;}
6. In order to enhance the readability of the program, so that the user of the function can easily know what exceptions are thrown by the function being used, you can list all the exception types that the function might throw in the declaration of the function, for example:
void Fun () throw (a,b,c,d);
This indicates that the function fun () May and may only throw exceptions of type (A,B,C,D) and its subtypes.
If there is no interface declaration that includes an exception in the declaration of the function, this function can throw any type of exception, for example:
void Fun ();
A function that does not throw any type of exception can be declared in the following form:
void Fun () thow ();
"C++primer" Reading notes-Exception handling