You are on page 1of 23

WEEK TEN (10)

@ Jane Nteere 1 1
Chapter Objectives
• Learn what an exception is
• Learn about checked and
unchecked exceptions
• See how the following blocks are used
to handle exceptions:
– Try
– Catch
– finally

@ Jane Nteere 2
EXCEPTION
• Definition: an occurrence of an
undesirable situation that can be detected
during program execution
• When an Exception occurs the normal flow
of the program is disrupted and the
program/Application terminates abnormally,
which is not recommended, therefore, these
exceptions are to be handled.
@ Jane Nteere 3
EXCEPTION
• An exception can occur for many
different reasons.
• Examples
– Division by zero
– A user has entered an invalid data.
– Trying to open an input file that does not
exist

@ Jane Nteere 4
Handling Exception within a
Program
• Can use an if statement to handle
an exception
• However, suppose that division by zero
occurs in more than one place within
the same block
– In this case, using if statements may not be
the most effective way to handle the exception

@ Jane Nteere 5
Java’s Mechanism of Exception
Handling
• When an exception occurs, an object of
a particular exception class is
created
• Java provides a number of exception classes
to effectively handle certain common
exceptions such as division by zero, invalid
input, and file not found
• Division by zero is an arithmetic error and
is handled by the class
ArithmeticException
@ Jane Nteere 6
Java’s Mechanism of Exception
Handling (continued)
• When a division by zero exception occurs,
the program creates an object of the
class ArithmeticException
• When a Scanner object is used to input
data into a program, any invalid input
errors are handled using the class
InputMismatchException
• The class Exception (directly
or indirectly) is the superclass of all
the exception classes in Java
@ Jane Nteere 7
Java Exception Classes

@ Jane Nteere 8
Java Exception Classes
(continued)

@ Jane Nteere 9
try/catch/finally
• Block that might generate an exception
Statements
are placed in a try block
• The try block might also contain
statements that should not be executed if an
exception occurs
• The try block is followed by zero or more
catch blocks
• A catch block specifies the type of
exception it can catch and contains an
exception handler
@ Jane Nteere 10
try/catch/finally
• Block
The last catch block may or may not
be followed by a finally block
• Any code contained in a finally block
always executes, regardless of whether an
exception occurs, except when the
program exits early from a try block by
calling the method System.exit
• If a try block has no catch block, then
it
must
@ Jane Nteere have the finally block 11
try/catch/finally
Block (continued)

@ Jane Nteere 12
try/catch
Block

@ Jane Nteere 13
try/catchBlock

@ Jane Nteere 14
try/catch
Block

@ Jane Nteere 15
try/catch
Block
•If no exception is thrown in a try block, all
catch blocks associated with the try block
are ignored and program execution resumes
after the last catch block
•If an exception is thrown in a try block, the
remaining statements in the try block are
ignored
- The program searches the catch blocks in the
order in which they appear after the try block and
@looks
Jane Nteerefor 16

an appropriate exception handler


Order of catch
Blocks
• The heading of a catch block specifies
the type of exception it handles
• A catch block can catch either all
exceptions of a specific type or all types of
exceptions

@ Jane Nteere 17
Order of catch
Blocks
• If in the heading of a catch block you
declare an exception using the class
Exception, then that catch block can
catch all types of exceptions because the
class Exception is the superclass of all
exception classes
• In a sequence of catch blocks following a
try block, a catch block declaring an
exception of a subclass type should be placed
before catch blocks declaring exceptions of
a@superclass
Jane Nteere type 18
try/catch/finally
Block
• If the type of the thrown exception matches the
parameter type in one of the catch blocks,
the code of that catch block executes and
the remaining catch blocks after this
catch block are ignored
• If there is a finally block after the last
catch block, the finally block executes
regardless of whether an exception occurs
@ Jane Nteere 19
try/catch/finally
Block

@ Jane Nteere 20
CSC111:Jan 2018 Week 9@ Jane Nteere 20
Checked Exceptions
• Definition: any exception that can
be recognized by the compiler
• Examples
– FileNotFoundExceptions

@ Jane Nteere 21
Unchecked Exceptions
• Definition: exceptions that cannot be recognized
when the program compiles (must be checked
for by programmer)
• Examples
– Division by zero
– Array index out of bounds
• Syntax

• ExceptionType1, ExceptionType2, and


so on are names of exception classes
@ Jane Nteere 22
Exception-Handling Techniques
• Terminate program
– Output appropriate error message upon
termination
• Fix error and continue
– Repeatedly get user input
– Output appropriate error message until valid
value is entered

@ Jane Nteere 23

You might also like