C + + exception handling: Calls to system function terminate

Source: Internet
Author: User

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:

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

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:

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

      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.

    1. [CPP] View plaincopyprint? #include <iostream>    using namespace std;   Class X   {public   :    class Trouble {};   Note: the Declaration and definition of nested classes in a class, learning!!!      Class Small:public Trouble {};     Class Big:public Inheritance!!!     in the trouble {};//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.

Tags Index: