C++ exception handling
Exceptions are problems that occur during program execution. A C++ exception is a special case that occurs while the program is running, such as an attempt to divide by zero.
Exceptions provide a way to transfer control of a program. C++ exception handling involves three keywords: try, catch, throw.
- throw: When the problem occurs, the program will throw an exception. This is done by using the throw keyword.
- catch: Exceptions are caught by exception handlers where you want to handle the problem. The catch keyword is used to catch exceptions.
- try: The try code in the block identifies the specific exception that will be activated. It is usually followed by one or more catch blocks.
If there is a block that throws an exception, the method that catches the exception uses the try and catch keywords. The code in the try block that throws an exception is called the protection code. The syntax for using a try/catch statement is as follows:
If the try block throws different exceptions in different situations, you can try to list multiple catch statements to catch different types of exceptions.
Throw an exception
You can use the throw statement to throw an exception anywhere in the code block. The operand of a throw statement can be any expression, and the type of the result of the expression determines the type of the exception thrown.
The following is an example of throwing an exception when trying to divide by zero:
catch block follows the try block and is used to catch exceptions. You can specify the type of exception you want to capture, which is determined by the exception declaration in parentheses after the catch keyword.
The above code will catch an exception of type ExceptionName. If you want the catch block to handle any type of exception thrown by a try block, you must use an ellipsis ... in the parentheses of the exception declaration, as follows:
The following is an example that throws an exception divided by zero and catches the exception in the catch block.
Because we throw an exception of type const char* , we must use const char* in the catch block when the exception is caught. When the above code is compiled and executed, it produces the following results:
Division by zero condition!
C++ standard exception
C++ provides a set of standard exceptions, defined in <exception> In the program, we can use these standard exceptions in the program. They are organized in a parent-child hierarchy, as follows:
The following table is a description of each exception that appears in the above hierarchy:
|std::exception||This exception is the parent of all standard C++ exceptions.|
|std::bad_alloc||This exception can be thrown by new.|
|std::bad_cast||This exception can be thrown by dynamic_cast.|
|std::bad_exception||This is very useful when dealing with exceptions that are not expected in C++ programs.|
|std::bad_typeid||This exception can be thrown by typeid.|
|std::logic_error||Theoretically an exception that can be detected by reading the code.|
|std::domain_error||This exception is thrown when an invalid math field is used.|
|std::invalid_argument||This exception is thrown when an invalid argument is used.|
|std::length_error||This exception is thrown when std::string is too long.|
|std::out_of_range||This exception can be thrown by methods, such as std::vector and std::bitset<>::operator()。|
|std::runtime_error||Theoretically it is not possible to detect an exception by reading the code.|
|std::overflow_error||This exception is thrown when a mathematical overflow occurs.|
|std::range_error||This exception is thrown when trying to store a value that is out of range.|
|std::underflow_error||This exception is thrown when a mathematical underflow occurs.|
Define a new exception
You can define new exceptions by inheriting and overloading the exception class. The following example demonstrates how to implement your own exception using the std::exception class:
This will produce the following results:
MyExceptionCaught C++ Exception
Here, what() is a public method provided by the exception class that has been overloaded by all child exception classes. This will return the cause of the exception.