Analysis of C + + exceptions

Source: Internet
Author: User
Tags throw exception

Exception Handling: exception, which allows a function to find an error that it cannot handle, throws an exception, allowing the caller of the function to handle the problem directly or indirectly.

Traditional error handling methods

1, terminate the program. (such as paragraph errors, etc.)

2, return the error code.

3, return the legal value, let the program in some kind of illegal state. (Pit cargo)

4. Call a pre-set function that is called when an error occurs.

Although the problem can be solved, but there are flaws, but the introduction of exception handling can be a good solution to the problem.

Throwing and capturing of exceptions

1, the exception is thrown by throwing objects, the type of Object determines which processing code should be activated.

2. The selected processing code is the one that matches the object type in the call chain and is closest to the exception where it was thrown.

3. when an exception is thrown, the local storage object is freed, so the thrown object is returned to the system, and the throw expression initializes a special copy of the exception object (the anonymous object), the exception object is compiled and managed, and the exception object is revoked after it is passed to the corresponding catch handler.

Stack expansion

when an exception is thrown, the execution of the current function is paused, the corresponding matching catch clause is searched, and the layer is constantly looking for it when it is not found . if the stack of the main function is reached, there is still no match, then the program terminates.

After a matching catch clause is found and processed, it continues to execute after the catch clause.

The specific implementation is as follows:

#include <iostream>using namespace std; #include <string>class exception{public: Exception (int errid, const char* errmsg =  ""): _errid (Errid),  _errmsg (ERRMSG {}void what () const{cout <<  "Errid:"  << _errId << endl; cout <<  "ErrMsg:" &NBSP;&LT;&LT;&NBSP;_ERRMSG&NBSP;&LT;&LT;&NBSP;ENDL;} private:int _errid;//error code string _errmsg;//error message};//exception thrown void func1 (Bool isthrow)// Throw Exception Object {if  (Isthrow) throw exception (1,  "Throw Exception object");cout <<  "func1- > "&NBSP;&LT;&LT;&NBSP;ISTHROW&NBSP;&LT;&LT;&NBSP;ENDL;} VOID&NBSP;FUNC2 (Bool isthrowstring,bool isthrowint)//throw string and int object {if  (isthrowstring) throw  string ("Throw string Object"), if (isthrowint) throw 7;cout <<  "func2->"  <<  isthrowstring << " " &LT;&LT;ISTHROWINT&LT;&LT;&NBSP;ENDL;} Void test () {//exception throw TRY{&NBSP;&NBSp; //func1 (TRUE); FUNC2 (true, true); At this point only Func1 (true), because the FUNC1 () function has an exception//for the following program, do not execute FUNC2 () the exception of the second parameter throws//from debugging can be found that exception handling will jump out, Causes program execution interrupt Func1 (false); FUNC2 (true,true);} catch  (const string& errmsg) {cout <<  "Catch string object:"   << errmsg << endl;} catch  (Int errid) {cout <<  "Catch int object:"  << errId  << endl;} catch  (const exception& e) {e.what ();} catch  (...) {cout <<  "Unknown exception" &NBSP;&LT;&LT;&NBSP;ENDL;} cout <<  "Func ()" &NBSP;&LT;&LT;&NBSP;ENDL;}

Abnormal re- throw out

It is possible that a single catch cannot handle an exception completely, and after some correction is made, it is hoped that the outer call chain function will be processed, and the catch can be handled by re-throwing the exception to the higher-level function.

Exceptions and Constructors & destructors

1, the constructor to complete the construction and initialization of the object, you need to ensure that you do not throw an exception in the constructor, otherwise it may result in incomplete or incomplete initialization of the object.

2, the destructor is mainly to complete the cleanup of resources, you need to ensure that you do not throw an exception within the destructor, otherwise it may lead to resource leaks (memory leaks, handles are not closed, etc.)

This article is from the "Materfer" blog, make sure to keep this source http://10741357.blog.51cto.com/10731357/1754989

Analysis of C + + exceptions

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.