1. exception Handling in C + +
(1)Catch statement block can throw an exception
Exceptions caught in ①catch can be re-thrown
② throws an exception that requires an outer try-catch block to capture
③catch (...) The method of throwing exceptions in a block is throw, and all exceptions are thrown again
(2) meaning of re-throwing exception in Catch block
① can be captured by the outer try-catch block and re-interpret the exception's information .
② Use this way to unify exception types in engineering development
A. Suppose our private library uses a 3rd-party library function , such as Func.
B. But the type of exception it throws is int* type and very unfriendly. We can catch this exception where the private library uses func, and re-interpret the exception in the catch block and throw it to our custom exception type .
The re-interpretation of the "Programming Experiment" exception
#include <iostream>using namespacestd;//demonstrates that an exception can be thrown in a catch blockvoidDemo () {Try { Try { Throw 'C'; } Catch(inti) {cout<<"inner:catch (int i)"<<Endl; ThrowI//To re-throw an exception } Catch(...) {cout<<"inner:catch (...)"<<Endl; Throw;//throws all types of exceptions } }Catch(...) {cout<<"outer:catch (...)"<<Endl; }}/*assume that the current function is a function in a third-party library. Therefore, we cannot modify the source code function name: void func (int i) throws exception type: int-1 ==> parameter exception-2 = = = Run exception-3 ==> Timeout exception*/voidFuncinti) { if(I <0 ) { Throw-1; } if(I > - ) { Throw-2; } if(i = = One) { Throw-3; } //normal Operationcout <<"Call func (int i)"<<Endl;}//Here is our private library, which uses the 3rd party library, where the 3rd party library's//exception types are unified into our exception Information formatvoidMyFunc (inti) { Try{func (i); } Catch(inti) {Switch(i) { Case-1: Throw "Invalid Parameter"; Break; Case-2: Throw "Runtime Exception"; Break; Case-3: Throw "Timeout Exception"; Break; } }}intMain () {Demo (); cout<<Endl; Try{MyFunc ( One); } Catch(Const Char*CS) {cout<<"Exception Info:"<< CS <<Endl; } return 0;};/*Output Result: Inner:catch (...) Outer:catch (...) Exception info:timeout Exception*/
2. Custom Exception class types
(1) For class type exception matching is still the first and the next strict match
(2) The assignment compatibility principle still applies in exception matching, so generally:
① matching subclass exception catch on top
② that match the parent exception are placed in the lower part (otherwise, if placed above, the subclass exception is captured by the parent class because of assignment compatibility).
(3) A series of exception classes are defined in the project, and each class represents an exception type that may occur.
(4) code reuse may require the re- interpretation of different exception classes (for example, due to inherited hierarchical relationships, it is likely that the parent class's exception class will be re-interpreted in the subclass)
(5) A reference is recommended as a parameter when defining a catch statement block
Exceptions for the "Programming experiment" class type
3.c++ Exception class family provided in the standard library
(1) Exceptions in the standard library (#include <stdexcept>) are derived from the exception class
(2) The exception class has two main branches
①logic_error : Avoidable logic errors that are commonly used in programs
②runtime_error : Commonly used in programs to avoid malignant errors
(3) Exceptions in the standard library
Exception usage in the "Programming Experiment" standard library
4. finally block solution not available in C + + exception mechanism
(1)Raii technology (Resource aquisition is initialization, resource acquisition is initialized )
① Basic Idea : the resource is represented by a local object , so the destructor of the local object will release the resource. That is, the resource is encapsulated into a class , the initialization of the resource is encapsulated in the constructor, and the release is encapsulated in the destructor. a local object is instantiated when the resource is to be used .
② When an exception is thrown , because the local object is out of scope and the destructor is automatically called , the resource is guaranteed to be freed.
(2) Specific practices
① directly uses local variables.
Try { File f ("xxx.ttt"/ / Use local object // Other actions } // exception occurs and normally, file resources are released here catch { // ...}
② to encapsulate resources in a class
classtest{ Public: File*file;//The resources are encapsulated.Test () {file =Newfile (...);} ~test () {Deletefile;}};Try{Test T;//working with local objects//Other Operations}//in any case, T is released here, and the destructor is called at the same timeCatch { //...}
③ using smart pointers
Try { std::auto_ptrnew file (); // ....} // in whatever case, the smart pointer is released here. Catch (...) { //....}
5. Summary
(1) exception can be thrown in a catch statement block
(2) The type of the exception can be a custom class type
(3) The assignment compatibility principle still applies in exception matching
(4) Exceptions in the standard library are derived from the exception class .
Lesson 65th exception Handling in C + + (bottom)