Curriculum
Multiple Catch and finally Block in Java are important exception handling features used to manage multiple runtime errors and ensure critical code execution. These concepts help developers build stable, secure, and reliable Java applications.
In this Core Java course in Jaipur, students will learn multiple catch blocks in Java, finally block, exception hierarchy, execution flow, cleanup operations, and practical examples used in software development.
Multiple catch and finally blocks are widely used in:
Understanding multiple catch and finally block in Java helps students create professional applications with proper error handling and resource management.
Multiple catch blocks allow developers to:
Different exceptions require:
Multiple catch blocks help developers:
try {
// risky code
} catch(ExceptionType1 e) {
// handling code
} catch(ExceptionType2 e) {
// handling code
}
class MultipleCatchExample {
public static void main(String[] args) {
try {
int[] numbers = {10, 20, 30};
System.out.println(numbers[5]);
int result = 10 / 0;
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid Array Index");
} catch(ArithmeticException e) {
System.out.println("Cannot Divide by Zero");
}
}
}
Invalid Array Index
Here:
numbers[5]
causes:
ArrayIndexOutOfBoundsException
So:
ArithmeticException
is never executed because exception already occurred.
Execution flow:
Specific exceptions must appear before:
catch(ArithmeticException e) {
}
catch(Exception e) {
}
catch(Exception e) {
}
catch(ArithmeticException e) {
}
This causes:
Compilation Error
The:
finally
block contains code that always executes:
finally block is mainly used for:
try {
// risky code
} catch(Exception e) {
// handling code
} finally {
// always executes
}
class FinallyExample {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch(ArithmeticException e) {
System.out.println("Exception Handled");
} finally {
System.out.println("finally Block Executed");
}
}
}
Exception Handled
finally Block Executed
finally block executes even if no exception occurs.
class FinallyNoException {
public static void main(String[] args) {
try {
System.out.println("Inside Try");
} finally {
System.out.println("finally Executed");
}
}
}
Inside Try
finally Executed
finally block closes:
finally block closes:
finally block releases:
finally block:
class CompleteExample {
public static void main(String[] args) {
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array Error");
} catch(Exception e) {
System.out.println("General Error");
} finally {
System.out.println("Program Finished");
}
}
}
Array Error
Program Finished
Java supports nested exception handling.
class NestedFinally {
public static void main(String[] args) {
try {
try {
int result = 10 / 0;
} catch(ArithmeticException e) {
System.out.println("Inner Catch");
}
} finally {
System.out.println("Outer finally");
}
}
}
Inner Catch
Outer finally
Multiple catch blocks provide:
finally block provides:
General exceptions before specific exceptions cause compilation errors.
Avoid unnecessary empty finally blocks.
Resources may remain open if exception occurs.
Handling:
Managing:
Handling:
Managing:
Understanding these concepts helps students:
In this lesson, students learned:
These concepts are essential for Java programming, backend development, and enterprise software systems.
Multiple catch blocks handle different exceptions separately.
finally block always executes whether exception occurs or not.
finally block is used for cleanup operations like closing files and database connections.
Yes, finally block executes even if no exception occurs.
Incorrect order causes compilation errors.
finally blocks are used in file handling, database systems, and enterprise applications.
WhatsApp us