Title: Chapter Eight
1Chapter Eight
2Objectives
- Learn about exceptions and the Exception class
- How to purposely generate a SystemException
- Learn about traditional error-handling methods
- Learn about object-oriented exception-handling
methods
3Objectives
- How to use the Exception classs ToString()
method and Message field - How to catch multiple Exceptions
- How to use the finally block
- How to handle an Exception with a loop
4Objectives
- How to throw an Exception
- How to trace Exceptions through the call stack
- How to create your own Exception classes
5Understanding Exceptions
- An exception is any error condition or unexpected
behavior in an executing program - Certain errors are called exceptions because they
are not usual occurrences - The object-oriented technique used to manage
exceptions make up the group of methods known as
exception handling - In C, all exceptions are objects
6Understanding Exceptions
- Most exceptions you will use derive from two
classes that in turn derive from the Exception
class - The predefined Common Language Runtime exception
classes derived from SystemException - The user-defined application exception classes
you derive from ApplicationException
7Purposely Generating a SystemException
- You can deliberately generate a SystemException
exception by forcing a program to contain an
error
8Purposely Generating a SystemException
- The DivideByZeroException object below was
generated automatically by C - Just because an Exception occurs when an
Exception object is created, you dont
necessarily have to deal with it
9Understanding Object-Oriented Exception-Handling
Methods
- In object-oriented terminology, you try a
procedure that may not complete correctly - A method that detects an error condition or
Exception throws an Exception - The block of code that processes the error
catches the Exception
10Understanding Object-Oriented Exception-Handling
Methods
- When you write a block of code in which something
can go wrong, you can place the code in a try
block, consisting of - The keyword try
- An opening curly brace
- Statements that might cause Exceptions
- A closing curly brace
11Understanding Object-Oriented Exception-Handling
Methods
- You create a catch block with the following
elements - The keyword catch, followed by an opening
parenthesis, the Exception type, a name for an
instance of the Exception type, and a closing
parenthesis - An opening curly brace
- Statements that take the action you want to use
to deal with the error condition - A closing curly brace
12Understanding Object-Oriented Exception-Handling
Methods
- General form of a trycatch pair
13Using the Exception Classs ToString() Method and
Message Field
- Any Exception generated from within a try block
would be caught by a catch block whose argument
is an Exception type (e.g., Exception e) there
is no way to confirm the origin of the Exception - You can use the ToString() method to provide a
descriptive error message - The Exception class also contains a field named
Message that contains useful information about an
Exception
14Using the Exception Classs ToString() Method and
Message Field
- Output of UsingTheException program when user
enters 0 for second number
15Catching Multiple Exceptions
- You can place as many statements as you need
within a try block, and you can catch as many
different Exceptions as you want - If you place more than one statement in a try
block, only the first error-generating statement
throws an Exception - When multiple catch blocks are present, they are
examined in sequence until a match is found for
the Exception that occurred
16Catching Multiple Exceptions
- TwoErrors class with two catch blocks
17Catching Multiple Exceptions
- Output of TwoErrors program
18Catching Multiple Exceptions
- If you reverse the two try statements within the
TwoErrors class, the output of the program changes
19Catching Multiple Exceptions
- When you want to execute the same code, no matter
what type of Exception occurs, you can use only
one catch block, which receives type Exception
20Catching Multiple Exceptions
- The Exception class is the base class for all
Exception objects and therefore can reference all
Exception descendants - The catch block in the previous code accepts all
Exception argument types - When you list multiple catch blocks following a
try, you must be careful that some catch blocks
dont become unreachable
21Catching Multiple Exceptions
- Program with unreachable catch block
22Catching Multiple Exceptions
- Error message generated by UnreachableCatch
program
23Using the finally Block
- The code within a finally block executes whether
or not the try block identifies an Exception - Typically, the finally block is used to perform
clean-up tasks - When you include a finally block, you are assured
that the finally statements will execute before
the program is abandoned
24Using the finally Block
- General form of a trycatch block with a finally
block
25Handling an Exception with a Loop
- Different programs require different ways of
handling Exceptions - In some cases, the try-catch sequence could be
placed in a loop that continues to execute until
the code is successful
26Throwing Exceptions
- An advantage of using object-oriented
exception-handling techniques is the ability to
deal with Exceptions appropriately as you make
conscious decisions about how to handle them - When methods from other classes throw Exceptions,
they dont have to catch them - When you design your own classes that might cause
Exceptions, you should create them to throw the
Exception but not to handle it - Handling an Exception should be left to the
client (the program that uses the class)
27Throwing Exceptions
- Two executions of TrySoccerPlayer1 program
28Throwing Exceptions
- Execution of TrySoccerPlayer2 program
29Tracing Exceptions Through the Call Stack
- The memory location where the computer stores the
list of locations to which the system must return
(after method calls) is known as the call stack - When a method throws an Exception, if the method
does not catch it, then the Exception is thrown
to the next method up the call stack - You can print the value of the StackTrace field
to display a list of methods in the call stack so
you can determine the location of the Exception
30Tracing Exceptions Through the Call Stack
- The StackTrace field can be a useful debugging
tool
31Creating Your Own Exception Classes
- You can create your own customized Exception
class for your application - To create your own Exception that you can throw,
you should extend the ApplicationException class - You should not create an excessive number of
special Exception types for your class because it
adds a level of complexity to your program
32Chapter Summary
- An exception is any error condition or unexpected
behavior in an executing program - You can purposely generate a SystemException
exception by forcing a program to contain an
error - When you think an error will occur frequently, it
is most efficient to handle it in the traditional
way, with if statements - In object-oriented terminology, you try a
procedure that may not complete correctly
33Chapter Summary
- Every Exception object contains a ToString()
method and a Message field - You can place as many statements as you need
within a try block, and you can catch as many
different Exceptions as you want - When you have actions to perform at the end of a
trycatch sequence, you can use a finally block - When you want to keep trying a block of code
until some value or state within a program is
correct, you can place a trycatch block within a
loop
34Chapter Summary
- When methods throw Exceptions, they dont have to
catch them instead, the program that calls a
method that throws an Exception can catch it and
determine what to do - When a method throws an Exception, if the method
does not catch it, then the Exception is thrown
to the method that called the offending method - To create your own Exception that you can throw,
you should extend the ApplicationException class