Exception Handling Summary (II)-exception Specification

Source: Internet
Author: User

Exception specification provides a solution to list the exceptions that a function may throw with the function declaration, so that the function does not throw any other exceptions.
An exception specification violation will not be checked during compilation, but will only be detected during runtime. For example:
Void func () Throw (string, INT );
Void doit () Throw (string)
{
Func (); // compiled
}
However, if the func function throws an int type exception during running, an exception specification violation occurs. Automatically call the running library function unexpected (). The default behavior of unexpected () is to call terminate () to terminate the program.

In addition, there must be consistency between exception specifications, such as the application in function pointers:
Void funcint () Throw (INT ){};
Void funcnull () Throw (){};
Void funcboth () Throw (char, INT ){};

Void (* pf1) () Throw (INT) = & funcint; // OK. Exception specifications are the same
Void (* pf2) () Throw (INT) = & funcnull; // OK. Exception specification compatibility
Void (* PF3) () Throw (char) = & funcboth; // error. The exception specification is incompatible.
The same is true in class inheritance:
// Exception classes
Class baseerr {};
Class printerr: Public baseerr {};
Class othererr {};
Class
{
Public:
Virtual void func1 () Throw (printerr );
Virtual void func2 () Throw (baseerr );
Virtual void func3 () Throw (baseerr );
Virtual void func4 () Throw (printerr );
Virtual void func5 () Throw (baseerr );
Virtual void func6 () Throw (baseerr, othererr );
};
Class D: public
{
Public:
Void func1 () Throw (printerr); // OK.
Void func2 () Throw (othererr); // error.
Void func3 () Throw (printerr); // OK.
Void func4 () Throw (baseerr); // error.
Void func5 () Throw (baseerr, othererr); // error.
Void func6 () Throw (printerr); // OK.
};
The exception specification declaration will cause some costs like the try/Catch Block. Therefore, the exception specification statement should be used wisely, just like other abnormal parts. When you see an exception specification statement at any time, you should convert them into try/catch queues in your mind to understand the relevant costs correctly.

Exception specification provides a solution to list the exceptions that a function may throw with the function declaration, so that the function does not throw any other exceptions.
An exception specification violation will not be checked during compilation, but will only be detected during runtime. For example:
Void func () Throw (string, INT );
Void doit () Throw (string)
{
Func (); // compiled
}
However, if the func function throws an int type exception during running, an exception specification violation occurs. Automatically call the running library function unexpected (). The default behavior of unexpected () is to call terminate () to terminate the program.

In addition, there must be consistency between exception specifications, such as the application in function pointers:
Void funcint () Throw (INT ){};
Void funcnull () Throw (){};
Void funcboth () Throw (char, INT ){};

Void (* pf1) () Throw (INT) = & funcint; // OK. Exception specifications are the same
Void (* pf2) () Throw (INT) = & funcnull; // OK. Exception specification compatibility
Void (* PF3) () Throw (char) = & funcboth; // error. The exception specification is incompatible.
The same is true in class inheritance:
// Exception classes
Class baseerr {};
Class printerr: Public baseerr {};
Class othererr {};
Class
{
Public:
Virtual void func1 () Throw (printerr );
Virtual void func2 () Throw (baseerr );
Virtual void func3 () Throw (baseerr );
Virtual void func4 () Throw (printerr );
Virtual void func5 () Throw (baseerr );
Virtual void func6 () Throw (baseerr, othererr );
};
Class D: public
{
Public:
Void func1 () Throw (printerr); // OK.
Void func2 () Throw (othererr); // error.
Void func3 () Throw (printerr); // OK.
Void func4 () Throw (baseerr); // error.
Void func5 () Throw (baseerr, othererr); // error.
Void func6 () Throw (printerr); // OK.
};
The exception specification declaration will cause some costs like the try/Catch Block. Therefore, the exception specification statement should be used wisely, just like other abnormal parts. When you see an exception specification statement at any time, you should convert them into try/catch queues in your mind to understand the relevant costs correctly.

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.