1 Exception Handling(Unit 2)
1 Exception Handling(Unit 2)
Exception Handling
Exception: An unexpected event that disrupts the normal flow of the program.
It is an object which is thrown at runtime. Java provides:
1.Inbuilt (Predefined) Exceptions 2. User-Defined (Custom) Exceptions
Exception handling: Mechanism to handle runtime errors so that normal flow of
application can be maintained.
Core advantage: to maintain the normal flow of the application.
A Output:
A
B C
C D Cont….
Flow Chart
JVM reaction to Exceptions
JVM Flow:
catch block
- used to handle the Exception.
- It must be used after the try block only.
- can use multiple catch block with a single try.
Problem without exception handling
• There can be 100 lines of code after exception. So all the code after exception will
not be executed.
Solution by exception handling
Using try-catch block:
try{
int a[]=new int[5];
a[5]=4;
} catch(ArrayIndexOutOfBoundsException e) {System.out.println(e);}
System.out.println("other statement”);
} catch(Exception e){System.out.println("handeled");} System.out.println("normal flow..");
} }
finally block
• used to execute important code such as closing connection, stream etc.
• always executed whether exception is handled or not.
• follows try or catch block.
class TestFinallyBlock{
Ex: public static void main(String args[]){
try {
//int data=25/5; //run these 2 lines one at a time to check finally
int data=25/0;
System.out.println(data); }
catch(ArithmeticException e)
{ System.out.println(e); }
finally
{ System.out.println("finally block is always executed"); }
System.out.println("rest of the code...");
}
}
throw keyword
• used to explicitly/manually throw an exception. Can throw either checked or unchecked
exception. It is mainly used to throw custom exception.
Ex 1: create the validate method that takes integer value as a parameter. If the age is less than 18, we are
throwing the ArithmeticException otherwise print a message welcome to vote.
import java.util.Scanner;
class TestThrow2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Please enter your roll number");
int roll = s.nextInt();
try {
if (roll < 0) {
throw new ArithmeticException("The number entered is not positive");
} else {
System.out.println("Valid roll number");
}
} catch (ArithmeticException e) {
System.out.println("An exception is thrown");
System.out.println(e.getMessage());
} }}
throws keyword
- used to declare an exception.
- gives an information to the programmer that there may occur an exception, thus, provide
the exception handling code so that normal flow can be maintained.
Syntax :
//method code
}
Example throws Keyword
Here, we have a method that can throw Arithmetic exception so we mentioned that with the method
declaration and catch that using the catch handler in the main method.
public class Ex {
static void checkAge(int age) throws ArithmeticException {
if (age < 18) {
throw new ArithmeticException("You are not eligible to vote.");
} else {
System.out.println("You are eligible to vote.");
}
}
throw throws
used to declare an exception possible
used to throw an exception explicitly.
during its execution.
is followed by an instance of Throwable is followed by one or more Exception
class or one of its sub-classes. class names separated by commas.
is used with method signature (method
is declared inside a method body.
declaration).
We can declare multiple exceptions
We cannot throw multiple exceptions
(separated by commas) using throws
using throw keyword.
keyword.
Common Inbuilt (Predefined) Exceptions :
Exception Meaning
Sometimes, we need to create our own exception class — to throw meaningful errors in
specific situations (like bank rules, school systems, voting, exams etc.)