1. Concept
Exception specification. If the function throws an exception, the thrown exception is included inOne ofOr from the listed exceptions.Derived type.
2. Definition
Exception description follows the function parameter table. An exception description follows the keyword throw followed by a list of abnormal types enclosed by parentheses (which may be null). For example:
Void Foo (INT) Throw (STD: logic_error, STD: runtime_error );
This statement indicates that foo is a function that accepts the int value and returns void. if Foo throws an exception, it will be a STD: logic_error or STD: runtime_error object, or an exception of the type derived from STD: logic_error or STD: runtime_error.
3. Description
- During compilation, the compiler cannot and will not attempt to verify the exception description;
- The empty description list indicates that the function does not throw any exception;
- If no exception is specified in a function declaration, the function can throw any type of exception;
- Exception description is part of the function interface. The function definition and any declaration of the function must have the same exception description;
- Exception description cannot be used for function overloading;
- In the const member function declaration, the exception description follows the const qualifier;
- If a function throws an exception not listed in its exception description, it calls the standard library function unexpected. By default, the unexpected function calls the terminate function, which generally terminates the program;
- Exception description/specification should include not only exceptions caused by functions, but also exceptions caused by other functions called by the function, and so on;
- When an exception is thrown, the compiler always creates a temporary copy, even if the Catch Block specifies a reference (the purpose of specifying a reference here is not to avoid creating a copy to improve efficiency,: the base class reference can be used to execute the derived class object .). for example:
Class problem {...}; <br/>... <br/> void super () Throw (problem) <br/> {<br/>... <br/> If (oh_no) <br/>{< br/> problem oops; // construct object <br/> throw oops; // throw it <br/>}< br/>... <br/>}< br/>... <br/> try <br/>{< br/> super (); <br/>}< br/> catch (problem & P) <br/>{< br/> // statements <br/>}< br/> // Note: P points to the copy of OOPS instead of oops. this is a good thing, because after the Super () function is executed, oops will no longer exist. however, it may be simpler: <br/> // throw problem ();
4. Code
// Usr_0000.h -- Exception classes for hmean (), gmean () </P> <p> # include <iostream> </P> <p> class bad_hmean <br/>{< br/> PRIVATE: <br/> double V1; <br/> double V2; <br/> Public: <br/> bad_hmean (double A = 0, double B = 0): V1 (A), V2 (B) {}< br/> void mesg (); <br/>}; </P> <p> inline void bad_hmean: mesg () <br/>{< br/> STD: cout <"hmean (" <V1 <"," <V2 <"): "<br/> <" invalid arguments: A =-B/N "; <br/>}</P> <p> class bad_gmean <br/>{< br/> Public: <br/> double V1; <br/> double V2; <br/> bad_gmean (double A = 0, double B = 0): V1 (A), V2 (B) {}< br/> const char * mesg (); <br/>}; </P> <p> inline const char * bad_gmean: mesg () <br/>{< br/> return "gmean () arguments shoshould be> = 0/N "; <br/>}
// Foo. CPP -- unwinding the stack </P> <p> # include <iostream> <br/> # include <cmath> // or math. h, unix users may need-LM flag <br/> # include <cstring> <br/> # include "usr_0000.h" </P> <p> class demo <br/> {< br/> PRIVATE: <br/> char word [40]; <br/> Public: <br/> demo (const char * Str) <br/>{< br/> STD :: strcpy (word, STR); <br/> STD: cout <"Demo" <word <"created/N "; <br/>}< br/> ~ Demo () <br/>{< br/> STD: cout <"Demo" <word <"destroyed/N "; <br/>}< br/> void show () const <br/>{< br/> STD: cout <"Demo" <word <"lives! /N "; <br/>}< br/>}; </P> <p> // function prototypes <br/> double hmean (double A, double B) throw (bad_hmean); <br/> double gmean (double A, double B) Throw (bad_gmean); <br/> double means (double A, double B) Throw (bad_hmean, bad_gmean); </P> <p> int main () <br/>{< br/> Using STD: cout; <br/> Using STD: CIN; <br/> Using STD: Endl; </P> <p> double X, Y, Z; <br/> demo D1 ("found in main ()"); <br/> cout <"Enter t Wo numbers: "; <br/> while (CIN> x> Y) <br/>{< br/> try <br/> {// start of try block <br/> Z = means (x, y ); <br/> cout <"the mean of" <x <"and" <Y <br/> <"is" <z <Endl; <br/> cout <"Enter next pair:"; <br/>}// end of try block <br/> catch (bad_hmean & BG) // start of Catch Block <br/>{< br/> BG. mesg (); <br/> cout <"Try again. /n "; <br/> continue; <br/>}< br/> C Atch (bad_gmean & Hg) <br/>{< br/> cout <Hg. mesg (); <br/> cout <"values used:" <Hg. v1 <"," <br/> <Hg. v2 <Endl; <br/> cout <"sorry, you don't get to play any more. /n "; <br/> break; <br/>}// end of Catch Block <br/>}< br/> d1.show (); <br/> cout <"Bye! /N "; </P> <p> return 0; <br/>}</P> <p> double hmean (double A, double B) Throw (bad_hmean) <br/>{< br/> if (a =-B) <br/>{< br/> throw bad_hmean (A, B ); <br/>}</P> <p> return 2.0 * a * B/(A + B ); <br/>}</P> <p> double gmean (double A, double B) Throw (bad_gmean) <br/>{< br/> if (a <0 | B <0) <br/>{< br/> throw bad_gmean (A, B ); <br/>}</P> <p> return STD: SQRT (A * B ); <br/>}</P> <p> double means (double A, double B) Throw (bad_hmean, bad_gmean) <br/>{< br/> double am, hm, GM; <br/> demo D2 ("found in means ()"); <br/> AM = (a + B)/2.0; // arithmetic mean <br/> try <br/>{< br/> Hm = hmean (a, B); <br/> GM = gmean (A, B ); <br/>}< br/> catch (bad_hmean & BG) // start of Catch Block <br/>{< br/> BG. mesg (); <br/> STD: cout <"caught in means ()/n"; <br/> throw; /// rethrows the exception <br/>}< br/> d2.show (); </P> <p> return (Am + HM + GM)/3.0; <br/>}