C++ Common exception handling principle and code sample analysis

  • 2020-10-23 20:15:27
  • OfStack

Common programming errors

Compiler errors - relatively easy to solve, mainly 1 syntax errors Program error - complex factors, such as lack of space, subscript overreach, access to illegal space, etc.

Exception refers to the abnormal occurrence of the program during operation, which can be divided into the following categories:

CPU exception; Such as in the calculation process, the divisor is 0.

Memory exceptions, such as:

Using new or malloc to apply for dynamic memory but not enough storage space;
Array subscript overbounds;
Use wild pointer and stray pointer to read memory;

Abnormal equipment, such as:

Unable to open the file, or the file is damaged; Moved a file or disk while reading a disk file; The printer is in use but the device is disconnected; The network in use is disconnected or blocked;

User data exceptions, such as:

scanf input data format or type error; There is an error in the database being processed. Changes in the data environment assumed by the program; Exception handling mechanism

Throw exceptions (throw), check exceptions (try block), catch exceptions (catch block)

C++ distinguishes exceptions by type, so when an exception is thrown, the value of the throw expression is meaningless, and the type of the expression is very important. If there are multiple exceptions to be thrown in a program, use different expression types to distinguish them from each other.

Notes on throw

When you execute throw, instead of executing the statement following throw, you move the program from throw to the matching catch, which can be catch in the same 1 function, or in the upper level function that directly or indirectly calls the exception function. The object thrown is an "exception object" initialized with an throw expression. The exception object is created by throw and initialized with a copy of the thrown expression. The exception object is passed to the corresponding catch and revoked when the exception handling is complete. So the exception object must be of a type that can be copied (with a copy constructor). If an array is thrown, the thrown object is automatically converted to a pointer to the first element of the array, and if a function is thrown, the function is converted to a pointer to that function. If a base class pointer to a derived class object is thrown, its object will be split and only parts of the base class will be thrown. It is always an error to throw a pointer to a local object, because when you throw a pointer, you must ensure that the object the pointer points to still exists when you enter the exception handler.

Detect catch exceptions

1. General form:


try{
  ....// Detect blocks (code that might throw an exception) 
}
catch( Exception specifier 1){
  ....// Handler (when the exception specifier 1 Program executed when thrown) 
}
catch( Exception specifier 2){
  ....// Handler (when the exception specifier 2 Program executed when thrown) 
}
..... // More and more catch

List of formal parameters for the catch clause

[

catch(type name) //catch only need to know the type of exception
catch(type name and parameter name) //catch needs to know more than the exception type
catch(...) Catch all exceptions

]

Heavy thrown exception

In the catch clause, you can throw an exception again, where throw does not add an expression to indicate that the caught exception will be thrown again to the parent function and will not be caught by other catch clauses of this function.


try{
  throw "hello"; // throw char*  abnormal   
}
catch(const char*){	// capture char* abnormal 
  throw;		// rethrow char*  Abnormal first 1 Level functions 
}

throw keyword - modified function

C++ function followed by the keyword throw(something) limit, is to limit the abnormal security of this function; This is an exception specification that only appears when a function is declared, indicating the type of exception that the function might throw.

[

void fun() throw(); // means that the fun function is not allowed to throw any exceptions, i.e. the fun function is exception-safe
void fun() throw(...) ; // means that the fun function can throw any type of exception
void fun() throw(exceptionType) // means that the fun function can only throw exceptions of type exceptionType

]

Such as void GetTag () throw (int); Means that exceptions of type int can only be thrown, and if an exception of type int is thrown, the unexsetpion() function is called and the program exits. If you use throw() qualifiers when a function is declared (the function itself cannot throw an exception), the compiler is more flexible in deciding how it is optimized.


Related articles: