Home > Backend Development > C#.Net Tutorial > C++ exception handling: call of system function terminate

C++ exception handling: call of system function terminate

php是最好的语言
Release: 2018-08-10 15:29:22
Original
2097 people have browsed it

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:

  1. [cpp] view plaincopyprint?
    #include <iostream>   
    #include <exception>   
    using namespace std;  
    void on_terminate(){  
    cout<<"terninate function called!"<<endl;  
     cin.get();  
    }  
    int main(void){  
     set_terminate(on_terminate);  
     throw exception();  
     cout<<"terminate function not called!"<<endl;  
     cin.get();  
    return 0;  
    }
    Copy after login

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:

  1. [cpp] view plaincopyprint?
    #include <iostream>   
    #include <exception>   
    using namespace std;  
    void on_terminate(){  
    cout<<"terminate function called!"<<endl;  
    cin.get();  
    }  
    class custom_exception{  
     custom_exception(){  
     }  
    custom_exception(const custom_exception& excep){  
     throw exception();  
     }  
    };  
    void case_1(){  
    try{  
      throw custom_exception();  
    }  
     catch(...){  
     }  
    }
    Copy after login

    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.

  1. [cpp] view plaincopyprint?
    #include<iostream>    
    using 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"<<endl;   
      }   
      catch(X::small&)   
      {   
        cout<<"caught small"<<endl;  
     }   
      catch(X::big&)   
      {   
      cout<<"caught big"<<endl;   
      }   
       return 0;   
    }
    Copy after login

    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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template