Exception Handling in Java
• What is Exception in Java?
• Dictionary Meaning: Exception is an abnormal condition.
• In Java, an exception is an event that disrupts the normal flow of the
program. It is an object which is thrown at runtime.
Hierarchy of Java Exception classes
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}catch(ArithmeticException e){[Link](e);}
//rest code of the program
[Link]("rest of the code...");
}
}
Exception in thread main [Link]:/ by zero
Multiple catch blocks
public class MultipleCatchBlock1 { catch(ArrayIndexOutOfBoundsException e)
{
public static void main(String[] args) {
[Link]("ArrayIndexOutOf
Bounds Exception occurs");
try{
}
int a[]=new int[5];
catch(Exception e)
a[5]=30/0;
{
}
catch(ArithmeticException e) [Link]("Parent Exception
occurs");
{
[Link]("Arithmetic Exception }
occurs"); [Link]("rest of the code");
}
}
}
Java Exception Keywords
Keyword Description
try The "try" keyword is used to specify a block where we should place an exception code. It
means we can't use try block alone. The try block must be followed by either catch or
finally.
catch The "catch" block is used to handle the exception. It must be preceded by try block which
means we can't use catch block alone. It can be followed by finally block later.
finally The "finally" block is used to execute the necessary code of the program. It is executed
whether an exception is handled or not.
throw The "throw" keyword is used to throw an exception.
throws The "throws" keyword is used to declare exceptions. It specifies that there may occur an
exception in the method. It doesn't throw an exception. It is always used with method
signature.
Java finally block
• Java finally block is a block used to execute important code such as closing
the connection, etc.
• Java finally block is always executed whether an exception is handled or not.
Loaded: 0.37%
Therefore, it contains all the necessary statements that need to be printed
Â
regardless of the exception occurs or not.
Why use Java finally block?
• finally block in Java can be used to put "cleanup" code such as closing a file,
closing connection, etc.
• The important statements to be printed can be placed in the finally block.
Case 1: When an exception does not occur
class TestFinallyBlock {
public static void main(String args[]){
try{
//below code do not throw any exception
int data=25/5;
[Link](data);
}
//catch won't be executed
catch(NullPointerException e){
[Link](e);
}
//executed regardless of exception occurred or not
finally {
[Link]("finally block is always executed");
}
[Link]("rest of phe code...");
}
}
Case 2: When an exception occur but not
handled by the catch block
public class TestFinallyBlock1{
//executes regardless of exception
public static void main(String args[]){
occured or not
finally {
try { [Link]("finally blo
ck is always executed");
[Link]("Inside the try block"); }
//below code throws divide by zero exception [Link]("rest of the
code...");
int data=25/0; }
[Link](data); }
}
//cannot handle Arithmetic type exception
//can only accept Null Pointer type exception
catch(NullPointerException e){
[Link](e);
}
Case 3: When an exception occurs and is
handled by the catch block
public class TestFinallyBlock2{
//handles the Arithmetic Exception / Divide by zero
public static void main(String args[]){ exception
catch(ArithmeticException e){
[Link]("Exception handled");
try { [Link](e);
}
[Link]("Inside try block") //executes regardless of exception occured or not
;
finally {
[Link]("finally block is always execut
//below code throws divide by zero e
xception ed");
}
int data=25/0;
[Link](data); [Link]("rest of the code...");
} }
}
• NOTE: For each try block there can be zero or more catch blocks,
but only one finally block.
• Note: The finally block will not be executed if the program exits
(either by calling [Link]() or by causing a fatal error that
causes the process to abort).
Java throw keyword
• The Java throw keyword is used to throw an exception explicitly.
• We specify the exception object which is to be thrown. The
Exception has some message with it that provides the error
description. These exceptions may be related to user inputs,
server, etc.
• We can throw either checked or unchecked exceptions in Java by
throw keyword. It is mainly used to throw a custom exception. We
will discuss custom exceptions later in this section.
Java throw Example
Java throws Example