C + + Exception understanding __c++

Source: Internet
Author: User
Error Handling Technology
1. Termination procedure (paragraph error)
2. Return error code
3. Return legal value
4. Call a preset error Si cho function (callback function)

Exception Handling
-Throws an exception when a function finds an error that it cannot handle, allowing the caller of the function to handle the problem directly or indirectly.

throw and catch of exceptions
-The exception is raised by throwing an object, and the type of the object determines which processing code should be activated.
-The processing code is the one that matches the object type and is closest to the thrown exception position
-Throws an exception to release the local storage object, the thrown object is ye Huan to the operating system, throw Initializes a special exception object (anonymous object), the exception object is compiled management, the object passes to the corresponding catch processing to undo. Stack Expansion
-Suspends execution of the current function while throwing an exception, and starts to find the corresponding catch.
-First in the throw itself whether within the member block, if it is to find catch, if there is a match, the processing, if there is no match, then exit the current function stack, continue to call the function of the stack to find, if the stack to main function still does not find a match, terminate the program. Matching rules for exception captures
-the Exception object type must exactly match the type of the catch
Exceptions :
1. From non-const object to const object.
2. Conversions from derived types to base class types.
3. The array is converted to a pointer to an array, and the function is converted to a pointer to a function.

#include <iostream> #include <string> using namespace std; Class Exception {public:exception (int errid, const char * errmsg): _errid (Errid), _errmsg (err
          MSG) {} void What () const {cout<< "Errid:" <<_errId<< Endl;
    cout<< "ErrMsg:" <<_errMsg<< Endl;       } Private:int _errid;  Error code string _errmsg;

Error message};
    void Func1 (bool isthrow) {//... if (isthrow) {throw Exception (1, "Throw Excepton object");
//... printf ("Func1 (%d) \ n", Isthrow); } void Func2 (bool isthrowstring, bool Isthrowint) {//... if (isthrowstring) {throw string ("
    Throw a String Object ");
    }//... if (isthrowint) {throw 7;
printf ("Func2 (%d,%d) \ n", isthrowstring, Isthrowint);
          } void Func () {try {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" << Endl;
printf ("Func () \ n");

    int main () {Func ();
return 0; }

The

results are as follows: the exception is thrown again
It is possible that a single catch cannot fully handle an exception, and after some correction is done, it is hoped to be handed over to the outer call chain function to handle it. Catch can be handled by re passing the exception to the upper-level function.

 #include <iostream> #include <string> using namespace std; Class Exception {public:exception (int errid = 0, const char * errmsg = ""): _errid (Errid), _
          ErrMsg (errmsg) {} void What () const {cout<< "Errid:" <<_errId<< Endl;
    cout<< "ErrMsg:" <<_errMsg<< Endl;       } Private:int _errid;  Error code string _errmsg;

Error message};

void Func1 () {Throw string ("throw Func1 string");}
    void Func2 () {try {Func1 ();
          catch (String & errmsg) {cout<<errmsg <<endl;
          Exception E (1, "Rethorw Exception");
          Throw e;
          Throw
    Throw errmsg;
    } void Func3 () {try {Func2 ();
    catch (Exception & E) {e.what ();
    int main () {Func3 ()};
return 0; }

The

results are as follows:
exception Constructor & destructor
Constructor completes the construction and initialization of an object, guaranteeing that an exception is not thrown in the constructor, or it may result in incomplete or incomplete initialization of the destructor The main completion of the resource cleanup, to ensure that no exception in the destructor, or cause a resource leak (memory leak, handle not closed, etc.).

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.