Module-4
Module-4
Exception Handling
Exceptions in Java
In Java, exceptions are events that disrupt the normal flow of a program during execution.
They are objects that represent an error or unexpected condition.
Why Use Exceptions?
To handle runtime errors gracefully
To maintain the flow of execution
To separate error-handling code from regular code
Types of Exceptions
1. Checked Exceptions
try {
} catch (IOException e) {
2. Unchecked Exceptions
Syntax
Exception Hierarchy
java.lang.Object
└── java.lang.Throwable
├── java.lang.Error
└── java.lang.Exception
├── Checked Exceptions
└── RuntimeException (Unchecked)
Common Exception Handling Keywords
Example: Try-Catch-Finally
public class Example {
public static void main(String[] args) {
try {
int[] arr = new int[3];
System.out.println(arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index is out of bounds!");
} finally {
System.out.println("This always executes.");
}
}
}
Multiple Catch Clauses in Java
In Java, a try block can be followed by multiple catch blocks to handle different types of
exceptions separately.
Why Use Multiple Catch Blocks?
To handle different exceptions differently
To maintain clean and readable error-handling logic
To catch specific exceptions before general ones
try {
// Code that may throw multiple exceptions
} catch (ArithmeticException e) {
System.out.println("Arithmetic error occurred.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index is invalid.");
} catch (Exception e) {
System.out.println("Some other exception occurred.");
}
Example: Handling Multiple Exceptions
public class MultiCatchExample {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
int result = 10 / 0; // ArithmeticException
System.out.println(numbers[5]); // ArrayIndexOutOfBoundsException
} catch (ArithmeticException e) {
System.out.println("Arithmetic error: " + e.getMessage());
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index error: " + e.getMessage());
} catch (Exception e) {
System.out.println("General error: " + e.getMessage());
}
}
}
Nested try Statement in Java
In Java, a nested try block means using a try block inside another try block. It allows for more
granular exception handling, especially when different pieces of code might throw different
exceptions.
Why Use Nested try Blocks?
To handle independent errors separately within the same scope
To catch specific exceptions where they occur
To prevent one exception from skipping the rest of the code
Syntax:
try {
// Outer try block
try {
// Inner try block
} catch (ExceptionType1 e1) {
// Inner catch block
}
} catch (ExceptionType2 e2) {
// Outer catch block
}
Example: Nested Try-Catch
public class NestedTryExample {
public static void main(String[] args) {
try {
// Outer try block
int[] arr = new int[3];
try {
// Inner try block
int result = 10 / 0; // ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Inner catch: Cannot divide by zero.");
}
System.out.println(arr[5]); // ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Outer catch: Array index out of bounds.");
}
System.out.println("Program continues...");
}
}
Output
Inner catch: Cannot divide by zero.
Outer catch: Array index out of bounds.
Program continues...
throw vs throws vs finally in Java
throw Keyword
Used to explicitly throw a single exception object (either built-in or custom).
Syntax:
throw new ExceptionType("error message");
Example:
public class ThrowExample {
public static void main(String[] args) {
int age = 15;
if (age < 18) {
throw new ArithmeticException("Access Denied - You must be 18+");
}
System.out.println("Access Granted!");
}
}
throws Keyword
Used in method declarations to indicate that the method might throw an exception.
Syntax:
returnType methodName() throws ExceptionType {
// code
}
Example:
import java.io.*;