Chapter 11 Exceptions

来源:互联网 发布:北京网络布线收费标准 编辑:程序博客网 时间:2024/06/05 05:34
. h
• 11.3 C++ Exceptions
• 11 .4 Throwing
Exceptions
• 11.4.1 Rethrown
Exceptions
• 11 .4.2 Exception
Expressions
• 11.5 Try Blocks
• 11.6 Handlers
• 11 .7 Exception
Specification
• 11.8 terminate()
and
unexpected()
• 11 .9 Example Exception
Code
• 11 .10 Standard
Exceptions and their Uses
• 11.11 Pragmatics
• Summary
• Exercises
Chapter 11 Exceptions
itseraoxhpC1cnp1 • This chapter describes exception handling
in C++. Exceptions
E xc e pt i on s
are generally
unexpected error conditions.
• Assertions
A s e r t i on s
are a program check for
correctness that forces an error exit when it
is violated. One point of view is that an
exception is based on a breakdown of a
contractual guarantee between the provider
of a code, the code's manufacturer, and what
the code is used for, the code's client.
Chapter 11 Exceptions
itseraoxhpC1cnp1 • In this model, the client needs to guarantee
that the conditions for applying the code
exist, and the manufacturer needs to
guarantee that the code will work correctly
under these conditions. In this methodology,
assertions provide the various guarantees.
11.1 Using assert
as s e r t
. h
h
• The user of the computation had the
responsibility of providing correct input.
This was a precondition.
pr e c on di t i on .
• The computation, if successful, satisfied a
postcondition
pos t c on di t i on
.
.
• void assert(bool expression)
e xpr e s i on )
11.1 Using assert
as s e r t
. h
h
• The down side is that the assertion
methodology does not allow a retry or other
repair strategy to continue program
execution.
• Also, assertions do not allow a customized
error message, though it would be easy to
add this capability.
11.2 Using signal.h
s i gn al .• The signal.h
s i gn al . h
file provides a standard
mechanism for handling system-defined
exceptions in a straightforward manner.
• The raise ()
function, prototyped in signal.h
s i gn al . h
,
,
can be used to generate an explicit
exception
11.2 Using signal.h
s i gn al .• sample code
11.2 Using signal.h
s i gn al .• Demo on page 360: signal.cpp
11.2 Using signal.h
s i gn al .
11.2 Using signal.h
s i gn al .
11.2 Using signal.h
s i gn al .• Demo on page 362: vect3.h vect4.h
11.2 Using signal.h
s i gn al .
11.3 C++ Exceptions
• The context for handling an exception is a
try
t r y
block.
• Handlers are declared at the end of a try
block using the keyword catch
c at c h
.
• C++ code can raise an exception in a try
t r y
block by using the throw
t h r ow
expression.
• The exception is handled by invoking an
appropriate handler selected from a list of
handlers found at the end of the handler's
try
t r y
block.
11.3 C++ Exceptions
• Demo on page 364: vect4.h
11 .4 Throwing Exceptions
• throw expression
t h r ow e xpr e s i on
• throw
t h r ow
• The throw
expression
e xpr e s i on
raises an exception.
• The throw
with no argument can be used
inside a catch
to rethrow
r e t h r ow
the current
exception.
• Dem on page 365: throw1.cpp
11 .4 Throwing Exceptions
11 .4 Throwing Exceptions
• Demo on page 366: throw2.cpp
11.4.1 Rethrown Exceptions
• Using throw
without an expression
rethrows
r e t h r ow s
a caught exception.
11 .4.2 Exception Expressions
• Frequently the handlers will not need this
information.
• In this case, it is appropriate to package the
information as an object:
11.5 Try Blocks
11.5 Try Blocks
• Throw Expression Matches the Catch
Handler Type if It Is
ilftIseraydTHnp
– 1. An exact match.
– 2. A
derived type of the public base class
handler type.
– 3. A thrown object type that is convertible to a
pointer type that is the catch
argument.
11.5 Try Blocks
• A t r y
block can be nested.
• If no handler can be found that matches,
then a default behavior is used This is by
default terminate()
(see Section 11.8,
"terminate0 and unexpected()," on page
370).
11.6 Handlers
11 .7 Exception Specification
• Syntactically, an exception specification is
part of a function declaration or function
definition, and has the form:
• function header throw (Type list)
f u n c t i on h e ade r t h r ow ( T y pe l i s t )
11 .7 Exception Specification
• Syntactically, an exception specification is
part of a function declaration or function
definition, and has the form:
• If an exception specification is left off, the
assumption is that an arbitrary exception
can be thrown by such a function.
Violations of these specifications are runtime
errors. They are caught by the function
unexpected().
11.8 terminate()
and unexpected()
• The system-provided function termi nate()
is called when no handler has been provided
to deal with an exception.
• The abort () function is called by default. It
immediately terminates the program,
returning control to the operating system.
• Another action can be specified by using
set-termi nate() to provide a handler.
• These declarations are found in except or
except.h.
11.8 terminate()
and unexpected()
• The system-provided handler unexpected0
is called when a function throws an
exception that was not in its exception
specification list.
• By default, the terminate() function is called.
• Otherwise, a set-unexpected() can be used
to provide a handler.
11 .9 Example Exception Code
• Demo on page 371: vect4.h vect4.cpp
11 .9 Example Exception Code
• Demo on page 371: vect4.h vect4.cpp
11 .9 Example Exception Code
11 .9 Example Exception Code
11 .9 Example Exception Code
11 .10 Standard Exceptions and their
Uses
11 .10 Standard Exceptions and their
Uses
• Standard exceptions are provided by C++
compilers and library vendors.
• Demo on page except.cpp (无库文件及类
型定义)
• A frequent use of standard exceptions is in
testing casts.
• Dem on page 374: bad_cast.cpp (程序需修
改)
11 .10 Standard Exceptions and their
Uses
• A frequent use of standard exceptions is in
testing casts.
11 .10 Standard Exceptions and their
Uses
11 .10 Standard Exceptions and their
Uses
• The standard library exceptions are derived
from the base class exception.
• Two derived classes are logic_error
and
runti
me-error.
• Logic error types include: bad_cast
t_,
out_of_range
agoeronuf
,
and bad-
typeid
iteydp
which are
intended to be
11 .10 Standard Exceptions and their
Uses
• thrown as indicated by their names. The
run-time error types include: range_error
agoern
,
overfl
lfrove
ow_error
oerw
,
and bad_a1 1oc.
.1_• The base class defines a virtual function:
• virtual const
charq
exception: :what()
const throw()
;
• The empty throw specification list indicates
that the function should not itself throw an
exception.
11.11 Pragmatics
• Paradoxically, error recovery is chiefly
concerned with writing correct programs.
• Exception handling is about error recovery.
It is also a transfer-of-control mechanism.
• Error recovery is based on the transfer of
control.
11.11 Pragmatics
• Useful exception handling is the disciplined
recovery when damage occurs.
• In most cases, programming that raises
exceptions should print a diagnostic
message and gracefully terminate.
• What can be agreed upon is that classes can
usefully be provided with error conditions.
• Assertions and simple throws and catches
that terminate the computation are parallel
techniques.
Summary
• 1. Exceptions are generally unexpected
error conditions.
• 2.
The standard library assert.h
as s e r t . h
provides the
macro
assert
(expression)
( e xpr e s i on )
;
• 3.
The signa1.h
s i gn a1. h
file provides a standard
mechanism for handling system-defined
exceptions in a straightforward manner.
Summary
• 4. C++ code can raise an exception by using
the throw
expression.
• 5. Syntactically, throws come in two forms:
throw
throw
expression
e xpr e s i on
Summary
6. Syntactically, a try
block has the form:
try
compound statement
c om pou n d s t at e m e n t
handler list
h an dl e r l i s t
• 7.
Syntactically, a handler has the form:
catch
(formal argument)
( f or m al ar gu m e n t )
compound statement
c om pou n d s t at e m e n t
Summary
• 8. Syntactically, an exception specification
is part of a function declaration, and has the
form:
function header throw (type list)
• 9. The system-provided handler terminate()
is called when no other handler has been
provided to deal with an exception.
compound statement
c om pou n d s t at e m e n 
原创粉丝点击