• Falsches Dokument? Kostenlos tauschen
  • Geschrieben von Student*innen, die bestanden haben
  • Sofort verfügbar nach Zahlung
  • Online lesen oder als PDF
Verkaufen
Wo studierst du
Deine Sprache
Document preview thumbnail
Vorschau 3 aus 25 Seiten
Notizen

Exception Handling and Inheritance

Document preview thumbnail
Vorschau 3 aus 25 Seiten

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

Inhaltsvorschau

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

Verknüpftes buch
 image
John Lewis, William Loftus Java Software Solutions, Global Edition
Herausgeber: 2018 ISBN: 9781292221724 Ausgabe: Unbekannt

Dokument Information

Hochgeladen auf
16. juni 2021
Anzahl der Seiten
25
geschrieben in
2020/2021
Typ
Notizen
Professor(en)
Dr simba
Enthält
Alle klassen
2,78 €

Falsches Dokument? Kostenlos tauschen Innerhalb von 14 Tagen nach dem Kauf und vor dem Herunterladen kannst du ein anderes Dokument wählen. Du kannst den Betrag einfach neu ausgeben.
Geschrieben von Student*innen, die bestanden haben
Sofort verfügbar nach Zahlung
Online lesen oder als PDF

Verkauft
1
Anhänger
1
Sachen
10
Zuletzt verkauft
5 Jahren vor



Warum sich Studierende für Stuvia entscheiden

on Mitstudent*innen erstellt, durch Bewertungen verifiziert

Geschrieben von Student*innen, die bestanden haben und bewertet von anderen, die diese Studiendokumente verwendet haben.

Nicht zufrieden? Wähle ein anderes Dokument

Kein Problem! Du kannst direkt ein anderes Dokument wählen, das besser zu dem passt, was du suchst.

Bezahle wie du möchtest, fange sofort an zu lernen

Kein Abonnement, keine Verpflichtungen. Bezahle wie gewohnt per Kreditkarte oder Sofort und lade dein PDF-Dokument sofort herunter.

Student with book image

“Gekauft, heruntergeladen und bestanden. So einfach kann es sein.”

Alisha Student

Arbeitest du an deiner Quellenangabe?

Erstelle korrekte Quellenangaben in APA, MLA und Harvard mit unserem kostenlosen Zitiergenerator.

Arbeitest du an deiner Quellenangabe?

Häufig gestellte Fragen