[ Pobierz całość w formacie PDF ]
becomes the function called by the corresponding special function: the argument to
set_unexpected() becomes the function called by unexpected(), and the argument
to set_terminate() becomes the function called by terminate().
Both set_unexpected() and set_terminate() return a pointer to the function that
was previously called by their respective special functions (unexpected() and
terminate()). By saving the return values, you can restore the original special
functions later so that unexpected() and terminate() will once again call
terminate() and abort().
If you use set_terminate() to register your own function, the function should no
return to its caller but terminate execution of the program.
If you attempt to return from the function called by terminate(), abort() is
called instead and the program ends.
Example of Using the Exception Handling Functions
The following example shows the flow of control and special functions
used in exception handling:
#include
#include
using namespace std;
class X { };
class Y { };
class A { };
// pfv type is pointer to function returning void
typedef void (*pfv)();
void my_terminate() {
cout
abort();
}
void my_unexpected() {
cout
throw;
}
void f() throw(X,Y, bad_exception) {
throw A();
}
void g() throw(X,Y) {
throw A();
}
int main()
{
pfv old_term = set_terminate(my_terminate);
pfv old_unex = set_unexpected(my_unexpected);
try {
cout
f();
}
catch(X) {
cout
Chapter 17. Exception Handling 335
Special Exception Handling Functions
}
catch(Y) {
cout
}
catch (bad_exception& e1) {
cout
}
catch (...) {
cout
}
cout
try {
cout
g();
}
catch(X) {
cout
}
catch(Y) {
cout
}
catch (bad_exception& e2) {
cout
}
catch (...) {
cout
}
}
The following is the output of the above example:
In first try block
Call to my_unexpected()
Caught bad_exception
In second try block
Call to my_unexpected()
Call to my terminate
At run time, this program behaves as follows:
1. The call to set_terminate() assigns toold_termthe address of the function last
passed to set_terminate() when set_terminate() was previously called.
2. The call to set_unexpected() assigns toold_unexthe address of the function last
passed to set_unexpected() when set_unexpected() was previously called.
3. Within the first try block, functionf()is called. Becausef()throws an
unexpected exception, a call to unexpected() is made. unexpected() in turn calls
my_unexpected(), which prints a message to standard output. The function
my_unexpected()tries to rethow the exception of typeA. Because classAhas not
been specified in the exception specification of functionf(),my_unexpected()
throws an exception of type bad_exception.
4. Because bad_exception has been specified in the exception specification of
functionf(), the handlercatch(bad_exception&e1)is able to handle the
exception.
5. Within the second try block, functiong()is called. Becauseg()throws an
unexpected exception, a call to unexpected() is made. The unexpected() throws
an exception of type bad_exception. Because bad_exception has not been
specified in the exception specification ofg(), unexpected() calls terminate(),
which calls the functionmy_terminate().
336 C/C++ Language Reference
Special Exception Handling Functions
6. my_terminate()displays a message then calls abort(), which terminates the
program.
Noe that the catch blocks following the second try block are not entered, because
the exception was handled bymy_unexpected()as an unexpected throw, not as a
valid exception.
v unexpected() on page 332
v terminate() on page 333
v set_unexpected() and set_terminate() on page 334
Chapter 17. Exception Handling 337
Special Exception Handling Functions
338 C/C++ Language Reference
Notices
This information was developed for products and services offered in the U.S.A.
IBM may not offer the products, services, or features discussed in this document in
other countries. Consult your local IBM representative for information on the
products and services currently available in your area.
Any reference to an IBM product, program, or service is not intended to state or
imply that only IBM product, program, or service may be used. Any functionally
equivalent product, program, or service that does not infringe any of IBM s
intellectual property rights may be used instead. However, it is the user s
responsibility to evaluate and verify the operation of any non-IBM product,
program, or service.
IBM may have patents or pending patent applications covering subject matter in
this document. The furnishing of this document does not give you any license to
these patents. You can send license inquiries, in writing, to:
Director of Licensing,
Intellectual Property & Licensing
International Business Machines Corporation,
North Castle Drive, MD - NC119
Armonk, New York 10504-1785,
U.S.A.
The following paragraph does not apply to the United Kingdom or any other
country where such provisions are inconsistent with local law:
INTERNATIONAL BUSINESS MACHINES CORPORATION PROVIDES THIS
PUBLICATION AS IS WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS
FOR A PARTICULAR PURPOSE. Some states do not allow disclaimer of express or
implied warranties in certain transactions, therefore, this statement may not apply
to you.
This information could include technical inaccuracies or typographical errors.
Changes are periodically made to the information herein; these changes will be
incorporated in new editions of the publication. IBM may make improvements
and/or changes in the product(s) and/or the program(s) described in this
publication at any time without notice.
Any references in this information to non-IBM Web sites are provided for
convenience only and do not in any manner serve as an endorsement of those Web
sites. The materials at those Web sites are not part of the materials for this IBM
product and use of those Web sites is at your own risk.
Licensees of this program who wish to have information about it for the purpose
of enabling: (i) the exchange of information between independent created programs
and other programs (including this one) and (ii) the mutual use of the information
which has been exchanged, should contact:
IBM Canada Ltd.
Department 071
1150 Eglinton Avenue East
© Copyright IBM Corp. 1998, 2001 339
Toronto, Ontario M3C 1H7
Canada
Such information may be available, subject to appropriate terms and conditions,
including in some cases payment of a fee.
The licensed program described in this information and all licensed material
available for it are provided by IBM under terms of the IBM Customer Agreement,
IBM International Program License Agreement, or any equivalent agreement
between us.
This publication contains examples of data and reports used in daily business
operations. To illustrate them as completely as possible, the examples include the
names of individuals, companies, brands, and products. All of these names are
fictitious and any similarity to the names and addresses used by an actual business
enterprise is entirely coincidental.
Programming Interface Information
This edition applies to Version 5, Release 1, Modification 0, of IBM WebSphere
Development Studio (5722-WDS), ILE C/C++ compilers.
This book is intended to help you create Integrated Language Environment C/C++
programs. It contains information necessary to use the Integrated Language
Environment C/C++ compiler. This book documents general-use programming
[ Pobierz całość w formacie PDF ]