• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 3 out of 25 pages
Class notes

Exception Handling and Inheritance

Document preview thumbnail
Preview 3 out of 25 pages

All applications encounter runtime errors. For example, a user may enter data that are 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 error 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

Content preview

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

Connected book
 image
John Lewis, William Loftus Java Software Solutions, Global Edition
Publisher: 2018 ISBN: 9781292221724 Edition: Unknown

Document information

Uploaded on
June 16, 2021
Number of pages
25
Written in
2020/2021
Type
Class notes
Professor(s)
Dr simba
Contains
All classes
R50,00

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
1
Followers
1
Items
10
Last sold
5 year ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions