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.