CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019
Lecture 5: Exception Handling and Inheritance
Objectives:
At the end of this lecture, students will be able to:
• define the term exception
• define the term inheritance
• use exception handling techniques to validate input
• use inheritance to create new classes
Notes:
All applications encounter runtime errors. For example, a user may enter data that's not appropriate for
the program, or a file that your program needs may get moved or deleted. These types of errors may
cause a poorly coded program to crash and cause the user to lose data. In contrast, when an erro occurs
in a well coded program, the program will notify the user, save as much data as possible, clean up
resources, and exit the program as smoothly as possible.
To help you handle errors, Java uses a mechanism known as exception handling. Before you learn
about how to handle errors, though, you need to learn about the exception hierarchy and the exception
handling mechanism.
The exception hierararchy
In Java, an exception is an object that's created from the Exception class or one of its sub-classes. An
exception represents an error that has occurred, and it contains information about the error. All excption
classes are derived from the Throwable class as shown here below.
The Throwable hierarchy
Throwable
Exception
Errot
Unchecked
errors RuntimeException
Checked
Unchecked exceptions
exceptions
Page 1 of 25
, CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019
Common checked exceptions
ClassNotFoundException
IOException
EOFException
FileNotFoundException
NoSuchMethodException
Common unchecked exceptions
ArithmeticException
IllegalArgumentException
NumberFormatException
IndexOutOfBoundsException
ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
NullPointerException
InputMismatchException
Description
• An exception is an object of the Exception class or any of its subclasses. It represents a
condition that prevents a method from successfully completing.
• The Exception class is derived from a class named Throwable. Two types of exceptions are
derived from the Exception class: Checked Exceptions and Unchecked exceptions.
• Checked exceptions are checked by the compiler. As a result, you must write code that handles
all checked exceptions before you can compiler your code.
• Unchecked exceptions are not checked by the compiler , but they can occur at runtime. It's
generally considered a good practice to write code that handles unchecked exceptions. If an
unchecked exception occurs and isn't handled by your code, your program will terminate.
• Like the Exception class, the Error class is also derived from the Throwable class. However, the
Error class identifies internal errors that are rare and can't usually be recovered from. As a result
you can usually ignore the Error class.
These classes in the Exception hierarchy are divided into tow categories:
1. Exceptions that are derived from the RuntimeException class and
2. all other exceptions.
The exceptions that are derived from the RuntimeException class are called unchecked exceptions
because the compiler doesn't force you to explicitly handle them. On the other hand, the compiler
requires that you explicitly handle all the other exceptions that are derived from the Exception class. As
a result, these exceptions are known as checked exceptions.
Unchecked exceptions often occur because of coding errors. For example, if a program attempts to
access an array with an invalid index, Java will throw an ArrayIndexOutOfBoundsException, which is
a type of IndexOutOfBoundsException. If you're careful when write your code, you can usually
Page 2 of 25
, CSI21M1 Java Programming Notes, prepared by Mr. S. Nyika, Semester I, 2019
prevent these types of exceptions from being thrown. Checked exceptions on the other hand, usually
occur due to circumstances that beyond the programmer's control, such as a missing file or a bad
network connection. Although you can't avoid these exceptions, you can write code that handles them
when they occur.
How exceptions are propagated
The diagram below shows how the exception handling mechanism works in Java.
methodA()
{ methodA()
{ try
{ try
{ methodB();
} methodB();
}
catch(ExceptionOne e)
{ catch(ExceptionOne e)
{ //handle
exception here //handle
exception
} here
} }
}
methodB() throws ExceptionOne
{ methodB() throws ExceptionOne
{ methodC();
} methodC();
}
methodC() throws ExceptionOne
{ methodC() throws ExceptionOne
{ methodD();
} methodD();
}
methodD() throws ExceptionOne
{ methodD() throws ExceptionOne
{ Throw new ExceptionOne();
} Throw new ExceptionOne();
}
Two ways to handle checked exceptions
• Throw the exception to the calling method
• Catch the exception and handle it
Page 3 of 25