Exception Handling in Java
Exception Handling in Java
Aniket Gupta
Roll no.: 02 B.Tech CSF
PRN :190105131067
Guided By – Geeta Desai ma’am
CONTENTS
What is an Exception ?
Error vs Exception
Hierarchy of Java Exception classes
Java Exception Handling Keywords
Types of Exception in Java
Internal working of java try-catch block
Exception Handler
Java Multi Catch block
The Throws / Throw Keywords
The finally block
Common scenarios where exceptions may occur
Sequence of Events for throw
Checked Exceptions
Un-Checked Exceptions
User-defined Exceptions
WHAT IS AN EXCEPTION?
Exception is an abnormal condition.
In java, exception is an event that disrupts the
Out of memory
Stack overflow
Thread Death
Linkage Error
I/O Exceptions
HIERARCHY OF JAVA EXCEPTION
CLASSES
KEYWORDS
There are 5 keywords used in java exception handling.
Try : Java try block is used to enclose the code that might
throw an exception. It must be used within the method. Java
try block must be followed by either catch or finally block.
Catch : Java catch block is used to handle the Exception. It
must be used after the try block only. We can use multiple
catch block with a single try.
Finally : Executes whether or not an exception is thrown in
the corresponding try block or any of its corresponding catch
blocks.
Throw : You can throw an exception, either a newly
instantiated one or an exception that you just caught, by
using the throw keyword.
Throws : If a method does not handle a checked exception,
the method must declare it using the throws keyword.
TYPES OF EXCEPTION IN JAVA
ArithmeticException
ArrayIndexOutOfBoundsException
ClassCastException
IllegalArgumentException
IndexOutOfBoundsException
NegativeArraySizeException
NullPointerException
NumberFormatException
NoSuchMethodException
INTERNAL WORKING OF JAVA
TRY-CATCH BLOCK
EXCEPTION HANDLER
Exception
“thrown”
here Thrown exception matched against
first set of exception handlers
Exception
Handler
If it fails to match, it is matched
against next set of handlers, etc.
Exception
Handler
If exception matches none of
handlers, program is abandoned
JAVA MULTI CATCH BLOCK
public class TestMultipleCatchBlock{
public static void main(String args[]){
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e){System.out.println("task1 is completed");}
catch(ArrayIndexOutOfBoundsException e)
{System.out.println("task 2 completed");}
catch(Exception e){System.out.println("common task completed");}
System.out.println("rest of the code...");
}
} At a time only one Exception is occurred and at a time only one catch
block is executed.
All catch blocks must be ordered from most specific to most general
i.e. catch for ArithmeticException must come before catch for Exception.
THE THROWS / THROW KEYWORDS
If a method does not handle a checked exception, the
method must declare it using the throws keyword. The
throws keyword appears at the end of a method's signature.
You can throw an exception, either a newly instantiated one
or an exception that you just caught, by using the throw
keyword.
***throws is used to postpone the handling of a checked
exception and throw is used to invoke an exception
explicitly.***
import java.io.*;
public class className
{ public void deposit(double amount) throws RemoteException
{ // Method implementation
throw new RemoteException();
}
//Remainder of class definition
}
THE FINALLY BLOCK
The finally block follows a try block or a catch block. A finally
block of code always executes, irrespective of occurrence of
an Exception.
Using a finally block allows you to 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 the catch blocks and has
the following syntax:
try
{ //Protected code
}catch(ExceptionType1 e1)
{ //Catch block }
catch(ExceptionType2 e2)
{ //Catch block }
finally { //The finally block always executes. }
COMMON SCENARIOS WHERE
EXCEPTIONS MAY OCCUR
ArithmeticException occurs
int a=50/0; //ArithmeticException
NullPointerException occurs
String s=null;
System.out.println(s.length());
//NullPointerException
NumberFormatException occurs
String s="abc";
int i=Integer.parseInt(s); //NumberFormatException
ArrayIndexOutOfBoundsException occurs
Try Block
Throw
statement
Unmatched
Catch
Matching Catch
Unmatched
Catch
Next Step
CHECKED EXCEPTIONS
Inherit from class Exception but not from
RuntimeException
Compiler enforces catch-or-declare
requirement
Compiler checks each method call and
method declaration
Checked exceptions are checked at compile-
time.
e.g. IOException, SQLException etc.
UNCHECKED EXCEPTIONS
Inherit from class RuntimeException or class
Error
Compiler does not check code to see if
below:
class MyException extends Exception{ }
Thank You