Chapter 11 Exceptions

Source: Internet
Author: User
. 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
Correctness that forces an error exit when it
Is violated. One point of view is that
Exception is based on a breakdown of
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
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
Responsibility of providing correct input.
This was a precondition.
Pr e c on di t I on.
• The computation, if successful, satisfied
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 wocould be easy
Add this capability.
11.2 using signal. h
S I gn Al. • The signal. h
S I gn Al. h
File provides a standard
Machism for handling system-defined
Specified tions 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
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
Appropriate handler selected from a list
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
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.
Derived type of the public base class
Handler type.
-3. A thrown object type that is convertible to
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
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,
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 Hangzhou T or
Invalid T. H.
11.8 terminate ()
And unexpected ()
• The system-provided handler unexpected0
Is called when a function throws
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 failed T. cpp (no library files and Classes
Type Definition)
• A frequent use of standard exceptions is in
Testing casts.
• DEM on page 374: bad_cast.cpp
Change)
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
Ageronuf
,
And bad-
Typeid
Iteydp
Which are
Intended to be
11. 10 standard exceptions and their
Uses
• Thrown as indicated by their names.
Run-time error types include: range_error
Agern
,
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 shocould not itself throw
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 mechanic.
• Error recovery is based on the transfer
Control.
11.11 pragmatics
• Useful exception handling is the disciplined
Recovery when damage occurs.
• In most cases, programming that raises
Exceptions shocould 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
Macro
Assert
(Expression)
(E xpr e s I on)
;
• 3.
The signa1.h
S I gn A1. H
File provides a standard
Machism for handling system-defined
Specified tions 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
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

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.