Chapter 4 - OOP For CS
Chapter 4 - OOP For CS
Exception Handling
By: Sinodos G.
1
Introduction
Exception is a problem that arises during the execution
of a program.
6
Cont’d …
All exception types are subclasses of class Throwable,
which is at the top of exception class hierarchy.
7
System errors
System errors are thrown by the JVM and represented in
the Error class.
The Error class describes internal system errors. Such errors
rarely occur.
If one does, there is little you can do beyond notifying the
user and trying to terminate the program gracefully.
VirtualMachineError
AWTError
8
Exceptions
Exceptions are represented in the Exception class,
which describes errors caused by your program and by
external circumstances.
These errors can be caught and handled by your
program.
9
Runtime exceptions
Runtime exceptions are represented in the Runtime
Exception class, which describes programming errors.
such as bad casting, accessing an out-of-bounds array, and
numeric errors.
NullPointerException
IndexOutOfBoundsException
IllegalArgumentException
10
Cont’d …
Based on these, we have three categories of
Exceptions:-
Checked exception
Unchceked Exception
Errors
12
Cont’d …
Checked exceptions:
an exception that occurs at the compile time, these are
also called as compile time exceptions.
Can’t simply be ignored at the time of compilation, the
programmer should take care of (handle) these exceptions.
Example:
Cont’d …
Unchecked exceptions:-
An exception that occurs at the time of execution.
Example:-
Handout
Exception Handling
declaring an exception,
19
Cont’d …
In Java, exception handling is done using five
keywords,
try
catch
throw
throws
finally
Exception handling is done by transferring the
execution of a program to an appropriate exception
handler when exception occurs.
20
Declaring Exceptions
In java when every method must state the types of
checked exceptions it might throw. This is known as
declaring exceptions.
System errors and runtime errors can happen to any
code, Java does not require that you declare Error and
RuntimeException (unchecked exceptions) explicitly in
the method.
21
22
Cont’d …
To declare an exception in a method, use the throws
keyword in the method declaration:-
Example:-
public void myMethod() throws IOException
The throws keyword indicates that myMethod
might throw an IOException.
Example:
Suppose the program detected that an argument
passed to the method violates the method contract
e.g:- the argument must be non-negative, but a
negative argument is passed;
30
Multiple catch Clauses
In some cases, more than one exception could be raised
by a single piece of code.
41
Creating Instance of Throwable class
There are two possible ways to create an instance of
class Throwable,
42
43
Cont’d …
throws Keyword
Any method that is capable of causing exceptions
must list all the exceptions possible during its
execution, so that anyone calling that method gets a
prior knowledge about which exceptions are to be
handled. A method can do so by using the throws
keyword.
Syntax:
45
Difference between throw and throws
throw throws
• throws keyword is used to
• throw keyword is used to
declare an exception
throw an exception
possible during its
explicitly.
execution.
• throw keyword is followed • throws keyword is followed
by an instance of Throwable by one or more Exception
class or one of its sub- class names separated by
classes. commas.
• throws keyword is used with
• throw keyword is declared
method signature (method
inside a method body.
declaration).
• We can declare multiple
• We cannot throw multiple
exceptions (separated by
exceptions using throw
commas) using throws
keyword.
keyword.
46
inally clause
A finally keyword is used to create a block of code that
follows a try block. A finally block of code is always
executed whether an exception has occurred or not.
Using a finally block, it lets you run any cleanup type
statements that you want to execute, no matter what
happens in the protected code. A finally block appears at
the end of catch block.
47
48
49
Cont’d …
User defined Exception
50
Cont’d …
51
Cont’d …
52
Question
53