May 11, 2021 C++
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 the program. There are three keywords involved in exception handling in C++: try, catch, throw.
If a block throws an exception, the method that catches the exception uses the try and catch keywords. T he try block places code that might throw an exception, and the code in the try block is called the protection code. The syntax for using try/catch statements looks like this:
try { // 保护代码 }catch( ExceptionName e1 ) { // catch 块 }catch( ExceptionName e2 ) { // catch 块 }catch( ExceptionName eN ) { // catch 块 }
If the try block throws different exceptions in different situations, you can try to list multiple catch statements at this time to catch different types of exceptions.
You can use the throw statement to throw an exception anywhere in the block of code. The number of operations for a throw statement can be arbitrary, and the type of result of the expression determines the type of exception thrown.
Here's an example of throwing an exception when trying to divide by zero:
double division(int a, int b) { if( b == 0 ) { throw "Division by zero condition!"; } return (a/b); }
The catch block follows the try block and is used to catch exceptions. You can specify the type of exception you want to catch, which is determined by the exception declaration in parentheses after the catch keyword.
try { // 保护代码 }catch( ExceptionName e ) { // 处理 ExceptionName 异常的代码 }
The above code catches an exception of type ExceptionName. If you want the catch block to handle any type of exception thrown by the try block, you must use an odd sign in the parentheses of the exception declaration ... as follows:
try { // 保护代码 }catch(...) { // 能处理任何异常的代码 }
Here is an instance that throws an exception divided by zero and catches the exception in the catch block.
#include <iostream>
using namespace std;
double division(int a, int b)
{
if( b == 0 )
{
throw "Division by zero condition!";
}
return (a/b);
}
int main ()
{
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
}catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
Since we threw an exception with a type of const char, when we caught the exception, we had to use const char in the catch block. When the above code is compiled and executed, it produces the following results:
Division by zero condition!
C++ provides a range of standard exceptions, defined in the . They are organized in a parent-child class hierarchy, as follows:
The following table is a description of each exception that appears in the above hierarchy:
abnormal | describe |
---|---|
std::exception | This exception is the parent class of all standard C ++ exceptions. |
std::bad_alloc | This exception can pass new Throw. |
std::bad_cast | This exception can pass dynamic_cast Throw. |
std::bad_exception | This is very useful when processed an exception that cannot be expected in the C ++ program. |
std::bad_typeid | This exception can pass typeid Throw. |
std::logic_error | It is theoretically detected by reading code. |
std::domain_error | This exception is thrown when an invalid mathematical domain is used. |
std::invalid_argument | This exception is thrown when an invalid parameter is used. |
std::length_error | This exception is thrown when you create too long std :: string. |
std::out_of_range | This exception can be thrown by way, such as std :: Vector and std :: bitset <> :: Operator [] (). |
std::runtime_error | Theoretically cannot detect exceptions by reading code. |
std::overflow_error | This exception is thrown when mathematically overflows. |
std::range_error | This exception will be thrown when trying to store a value of the range. |
std::underflow_error | This exception is thrown when a mathematical overflow occurs. |
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:
#include <iostream>
#include <exception>
using namespace std;
struct MyException : public exception
{
const char * what () const throw ()
{
return "C++ Exception";
}
};
int main()
{
try
{
throw MyException();
}
catch(MyException& e)
{
std::cout << "MyException caught" << std::endl;
std::cout << e.what() << std::endl;
}
catch(std::exception& e)
{
//其他的错误
}
}
This results in the following:
MyException caught C++ Exception
Here, what() is a common method provided by the exception class, which is overloaded by all sub-exception classes. This returns the cause of the exception.