Home>Article>Backend Development> C++ exception handling: call of system function terminate
The process of handling exceptions in C is as follows: when an exception occurs during the execution of the program, it does not need to be handled in this function. Instead, an error message is thrown and passed to the upper level function for resolution. The upper level resolves it. If it fails, it will be passed to the upper level for processing. Uploading in this way step by step, if it cannot be processed until the highest level, the running system will automatically call the system function terminate,
Learning to use the terminate function will help in exception handling
一 When a The terminate function is called when an exception occurs. Code:
[cpp] view plaincopyprint? #include#include using namespace std; void on_terminate(){ cout<<"terninate function called!"<
terminate is called:
1 When an exception is sent, And the constructor generates an exception
2 When an exception is sent, or the destructor generates an exception
3 When the construction or destruction of a static object sends an exception
4 When an exception occurs in a function registered with atexit
5 When customizing an exception, but no exception is actually generated
6 When calling the default unexcepted() function
Example:
[cpp] view plaincopyprint? #include#include using namespace std; void on_terminate(){ cout<<"terminate function called!"< When a function When a throw exception is thrown, if an object is constructed internally in the function, the system will first call the destructor of the object. After the object has called the destructor, it will start throwing the exception.
At the same time, among the exceptions of classes with inheritance relationships, the exceptions of the subclass should be placed at the front, and the exceptions of the base class should be placed at the end, so that the exceptions of the subclass can be The exceptions of the parent class are handled first, and the exceptions of the parent class are handled last.
[cpp] view plaincopyprint? #includeusing namespace std; class X { public: class Trouble {}; //注意:类中嵌套类的申明和定义,学习!!! class small: public Trouble {}; class big:public Trouble {};//类中的继承!!! void f(){ throw big(); } }; int main() { X x; try{ x.f(); } catch(X::Trouble &) { cout<<"caught Trouble"< If this is the case, the big() type exception thrown will be monopolized by the trouble class. You should write it backwards to capture all exceptions sequentially. In addition, you can use... to capture All exceptions should be placed at the end.
The ellipsis exception handler is not allowed to accept any parameters, so it is impossible to get any information about the exception, and it is impossible to know the type of exception. This kind of catch statement is often used to clean up resources and rethrow Exception caught.
Related recommendations:
Exception handling example usage
nginx php-fpm 502 bad gateway exception solution
The above is the detailed content of C++ exception handling: call of system function terminate. For more information, please follow other related articles on the PHP Chinese website!