0% found this document useful (0 votes)
5 views18 pages

Chapter 10 - Exception Handling

This document is a chapter on Exception Handling in Object Oriented Programming, specifically focusing on Java. It covers the overview of exceptions, their types, handling mechanisms, and the use of keywords like try, catch, finally, throws, and throw. Additionally, it discusses uncaught exceptions, multiple catch clauses, and methods for declaring and throwing exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

Chapter 10 - Exception Handling

This document is a chapter on Exception Handling in Object Oriented Programming, specifically focusing on Java. It covers the overview of exceptions, their types, handling mechanisms, and the use of keywords like try, catch, finally, throws, and throw. Additionally, it discusses uncaught exceptions, multiple catch clauses, and methods for declaring and throwing exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arba Minch University

College Of Medicine And Health Sciences

School Of Public Health

Department Of Health Informatics

Object Oriented Programming (OOP)

Chapter Ten
Exception Handling
Berihun M. Lecturer(BSc(Cs) , MSc(IT))
Chapter Outline
 Overview of Exceptions
 Exception Types (Sample Java’s Built-in Exceptions)
 Uncaught Exceptions
 Fundamentals of Exception Handling
 Using try-catch-finally Block
 Multiple catch Clauses
 Displaying Description of an Exception
 Declaring Exceptions Using throws Keyword
 Throwing Exceptions Using throw Keyword

2
Overview of Exceptions
 A Java exception is an object that describes an
exceptional (that is, error) condition that has
occurred in a piece of code.
 When an exceptional condition arises, an object
representing that exception is created and
thrown in the method that caused the error.
 Exceptions can be generated by the Java run-time
system, or they can be manually generated by
your code.

3
Exception Types in Java
 All exception types are subclasses of the built-in class
Throwable (from [Link] package).
 Immediately below Throwable are two subclasses:
1. Exception: indicates exceptional conditions that a
user program might want to catch.
🖒Enables you to create your own custom subclass of
exception types.
2. Error: indicates serious problems that cannot be
caught and handled by your program.
🖒Exceptions of type Error are used by the Java run-
time system to indicate errors having to do with
the run-time environment itself.
4
Portion of class Throwable’s inheritance hierarchy
5
Exception Types in Java(Cont’d…)
 RuntimeException, Error and their subclasses are
known as unchecked exceptions.
 The compiler does not check to see if a method or
constructor handles or throws these exceptions.
 Unchecked exceptions typically can be prevented
by proper coding.
 The class Exception and all classes that inherit from
class Exception but not directly or indirectly from
class RuntimeException are considered to be
checked exceptions.
 The Java compiler forces the programmer to check
and deal with checked exceptions.
6
Uncaught Exceptions
 If we haven’t supplied any exception handlers of our
own in a program, the exception is caught by the
default handler provided by the Java run-time
system.
 The default handler displays a string describing the
exception, prints a stack trace from the point at
which the exception occurred, and terminates the
program.
 The stack trace will always show the sequence of
method invocations that led up to the error.

7
Fundamentals of Exception Handling
 Exceptions can be thrown from a method or
constructor so that the caller of the method or
constructor can catch and handle the exception
itself, or pass it on.
 If the exception is not handled, the program will
terminate abnormally.
 Exception handling helps you write robust and fault-
tolerant programs that can deal with problems and
continue executing or terminate gracefully.
 Java exception handling is managed via five
keywords: try, catch, finally, throws, and throw.

8
Fundamentals of Exception Handling(Cont’d…)
 Statements that cause exceptions are enclosed within a
try block. If an exception occurs within the try block, it is
thrown.
 Immediately following the try block, include a catch
block that specifies the exception type thrown by try
that you wish to catch and handle.
 Any code that is always executed regardless of whether
an exception occurs or is caught is put in a finally block.
 Any exception that is thrown out of a method must be
specified by a throws clause.
 System-generated exceptions are automatically thrown
by the Java runtime system.
 To manually throw an exception, use the keyword throw.
9
Using try-catch-finally Block
 The try-catch-finally block syntax:
try {
// Statements that may throw exceptions
}
catch(ExceptionType1 exceptionVar1) {
// handler for ExceptionType1;
}
catch(ExceptionType2 exceptionVar2) {
// handler for ExceptionType2;
}
...
// The finally block must be placed after the last catch block.
finally {
// final statements, resource release codes
}
 Note: finally block is optional.
10
Multiple catch Clauses
 In some cases, more than one exception can be raised by
a single piece of code.
 To handle different types of exceptions, you can specify
two or more catch clauses.
 If an exception is thrown, each catch statement is
inspected in order.
 When you use multiple catch statements, it is important
to remember that exception subclasses must be
specified before any of their superclasses.
 This is because a catch statement that uses a superclass
will catch exceptions of that type plus any of its
subclasses.

11
Displaying Description of an Exception
 You can display description of an exception by passing
the exception object as an argument in an output
statement.
For example:
catch(ArithmeticException ae) {
[Link]("%Exception: %s%n", ae);
}
 You can also use Throwable methods printStackTrace to
output the stack trace to the standard error stream and
getMessage to return the descriptive string stored in an
exception.

12
Declaring Exceptions Using throws Keyword
 Every method that is capable of causing an exception
must state the types of checked exceptions it might
throw to its caller. This is known as declaring
exceptions.
 You do this by including a throws keyword in the
method header.
 Checked exceptions thrown by the method must be
explicitly declared in the method header’s throws
list, if that method does not handle them itself, so
that the caller of the method is informed of the
exception.
 Otherwise a compile-time error will occur.

13
Declaring Exceptions Using throws Keyword…
 This is the general form of a method declaration that
includes a throws clause:
[modifier] returnType methodName() throws exceptionList
{
// body of method
}
 Here, exceptionList is a comma-separated list of
the exceptions that a method can throw.
 Java does not require that you declare Error and
RuntimeException (unchecked exceptions) explicitly
in the method’s throws list.

14
Throwing Exceptions Using throw Keyword
 A program that detects an error can create an instance
of an appropriate exception type and throw it. This is
known as throwing an exception.
 The keyword to throw an exception explicitly is throw.
🖒 throw ThrowableObject;
There are two ways you can obtain a Throwable object:
🖞 using a parameter in a catch clause or
🖞 creating one with the new operator.
 Any subsequent statements immediately after the throw
statement are not executed.

15
Declaring, Throwing and Catching Exceptions

16
Catch or Declare Checked Exceptions
 If a method declares a checked exception, you must
invoke it in a try-catch block or declare to throw the
exception in the calling method.
Example:

17
Reading Assignment
 Nested try statements
 Create your own Exception subclasses
 Chained exceptions

18

You might also like