C + + exception handling: Calls to system function terminate

Source: Internet
Author: User
Tags throw exception
The process of handling exceptions in C + + is this: in the execution of the program exception, can not be processed in this function, but instead of throwing an error message, pass it to the previous level of the function to resolve, the upper level can not be resolved, and then passed to its upper level, by its upper level processing. The system will automatically invoke the Terminate function if it is not processed at the highest level.





Learn to use the Terminate function to help with exception handling



When an exception is generated, call the Terminate function, code:


[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;  

}


Terminate the condition that is called:



1 When an exception is sent and the constructor produces an exception
2 When an exception is sent, or the destructor produces an exception
3 The construction or destruction of a static object sends an exception
4 When an exception occurs for a function registered with Atexit
5 Custom an exception, but there is no exception when it is actually generated.
6 when calling the default unexcepted () function
Example Talk:


[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(...){  

 }  

}


When a function throws a throw exception, if the function constructs an object inside, the system will first call the destructor, and when the object has finished invoking the destructor, it starts to execute the exception throw work.


At the same time, in the exception of the class that has the inheritance relationship, the exception of the subclass should precede, and the exception of the base class should be put to the last side, so that the exception of the subclass gets processed first, and the exception of the parent class is processed.


[cpp] view plaincopyprint?

#include<iostream>

Using namespace std;

Class X

{

Public:

  Class Trouble {}; //Note: the declaration and definition of nested classes in the class, learn!!!

   Class small: public Trouble {};

   Class big:public Trouble {};//Inheritance in class!!!

  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;

}  


If so, the thrown big () type exception is trouble class Monopoly, should be written backwards to achieve the order to catch all exceptions, in addition to use ... Can catch all the exceptions, this should be put on the last side.


The ellipsis exception handler does not allow any parameters to be accepted, so it is not possible to get information about any related exceptions or to know the type of the exception, which is often used to clean up resources and to re-throw the caught exceptions.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.