Exception handling mechanism and exception specification in C + + __c++

Source: Internet
Author: User
Tags exception handling

The following code runs under 64-bit system VS2013
exception in C + +:
Syntax error (compilation error): such as variable undefined, parentheses mismatch, keyword spelling errors, etc. compiler can find errors at compile time, Such errors can be found in time by the compiler, but also in time to know the location and cause of the error, easy to correct.
Run-time Errors: For example, array subscripts are out of bounds, system memory is low, and so on. This kind of error is not easy to be discovered by the programmer, it can compile and can enter to run, but run error, cause program crashes.
in order to effectively handle program Run-time errors, an exception handling mechanism is introduced in C + + to resolve this problem.
C + + exception handling mechanism:
Exception handling basic idea: An exception is found in the process of executing a function, and instead of being processed immediately in this function, the exception is thrown so that the caller of the function can handle the problem directly or indirectly. The
C + + exception handling mechanism consists of 3 modules: try (check), throw (throw), catch (capture)
to throw an exception in the form of a throw expression, or throw an exception if an exception is found in a try block.
Try
{
The statement that might throw an exception; (check)
}
catch (type name [parameter name])//Catch a specific type of exception
{
//process 1;
}
catch (type name [formal parameter name])/ /captures a specific type of exception
{
//processing 2;
}
catch (...). //Captures all types of exceptions
{
}
Stack expansion:
has four functions F1 (), F2 (), F3 (), F4 (), F2 () in F1 (), F3 () F2 (), F4 () F3 (), Main () is F4 () and an exception is thrown in F1 (), caught in main () with a catch statement. The stack expansion process is as follows:

Reference Code:

void F1 (int i)
{
    if (i<0)
    throw 1;
}
void F2 ()
{
    F1 ( -45);
}
void F3 ()
{
    F2 ();
}
void F4 ()
{
    F3 ();
}
int main ()
{
    try{
        f4 ();
    }
    catch (int i)
    {
        cout << "error code:" << i << "\ n" << Endl;
    }
    System ("pause");
    return 0;
}

Run Result:

matching rules for exception captures:
Throw that type of exception inside a try block, what type of exception is caught inside the catch, in general, the type must match exactly, but the following 3 kinds of cases can be type conversion:
1. Allow conversions from non-const objects to Const.
That is, throw1 a non-const object, catch1 a const object
2. Conversions from derived class types to base class types are allowed.
That is, throw1 a derived class object, catch1 a base class object
3. Converts an array to a pointer to an array type, and the function is converted to a pointer to a function type.
unexpected re-throw:
The reference code is as follows:

void Fun (int i)
{
    if (I < 0)
        throw-1;
}
void Fun2 ()
{
    try
    {
        fun ( -23);
    }
    catch (int i)
    {
        cout << "Errcode:" << i << "\ n";
    }
}
int main ()
{
    fun2 ();
    System ("pause");
    return 0;
}

Run Result:

Run the above code, we can only know that the program is wrong, after the throw, we not only know that the program is wrong can also know the cause of the error, so as to facilitate timely correction.
The reference code is as follows:

void Fun (int i)
{
    if (I < 0)
        throw-1;
}
void fun2 (int i)
{
    try
    {
        fun (i);
    }
    catch (int i)
    {
        switch (i)
        {
        case-1;
            Throw "data out of bounds";//re-throw
        }}
int main ()
{
    try
    {
        fun2 ( -34);
    }
    catch (const char *s)
    {
        cout << "error code:" << s<< "\ n" << Endl;
    }
    System ("pause");
    return 0;
}

Run Result:

Exception Specification:
After a function declaration, listing the function may throw an exception type and ensure that the function does not throw other types of exceptions.
1, the reference code is as follows:

void Fun1 () throw (int)//indicates that only int-type exception
{
    throw ' 1 ' can be thrown.
}
int main ()
{
    fun1 ();
    System ("pause");
    return 0;
}

Warning at compile time: Ignore exception

2, if the constant specification is throw (), it means that no exception should be thrown.
exceptions and constructors and destructors:
1, guarantees that exceptions are not thrown in constructors, because constructors are constructed and initialized, and when an exception is thrown, it can cause incomplete or incomplete initialization of the object.
2 guarantees that no exception is thrown in the destructor because the function of the destructor is to clean up the resource and, once thrown, it can cause a resource leak (memory leak, and so on).

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.