100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Exception Handling and Inheritance $2.92   Add to cart

Class notes

Exception Handling and Inheritance

 1 view  0 purchase
  • Course
  • Institution
  • Book

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 e...

[Show more]

Preview 3 out of 25  pages

  • June 16, 2021
  • 25
  • 2020/2021
  • Class notes
  • Dr simba
  • All classes
avatar-seller
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

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller keanuperumal. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $2.92. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

83225 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling

Recently viewed by you


$2.92
  • (0)
  Add to cart