Program 6 : Multiple Exceptions
Demonstrate exception handling fundamentals in Java, Including catching multiple exceptions and using built-in exceptions.
java
public class MultipleExceptionDemo {
public static void main(String[] args) {
int a = 10;
int b = 0;
int result;
int[] nums = {1, 2, 3};
try {
// Causes ArithmeticException
result = a / b;
System.out.println("Result: " + result);
// Causes ArrayIndexOutOfBoundsException
nums[5] = 10;
// Causes NullPointerException
String str = null;
System.out.println(str.length());
} catch (ArithmeticException e) {
System.out.println("Caught ArithmeticException: " + e);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught ArrayIndexOutOfBoundsException: " + e);
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException: " + e);
}
System.out.println("After try-catch blocks.");
}
}
Updated
java
public class MultipleExceptionDemo {
public static void main(String[] args) {
int a = 10;
int b = 0;
int[] nums = {1, 2, 3};
System.out.println("--- Multiple catch Blocks ---");
try {
// Causes ArithmeticException
int result = a / b;
System.out.println("Result: " + result);
// This line is never reached
// Causes ArrayIndexOutOfBoundsException
nums[5] = 10;
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero. "
+ e.getMessage());
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Array index is out of bounds. "
+ e.getMessage());
} finally {
System.out.println("Finally block always runs.");
}
System.out.println("\n--- Multi-catch Block ---");
try {
String str = null;
// Cause a NullPointerException
System.out.println(str.length());
} catch (NullPointerException | ArrayIndexOutOfBoundsException e) {
System.out.println("Caught a runtime error: " + e.toString());
}
System.out.println("\nProgram has finished executing.");
}
}
java
public class MultipleExceptionDemo {
public static void main(String[] args) {
int a = 10;
int b = 0;
int[] nums = {1, 2, 3};
// Handle ArithmeticException
try {
int result = a / b;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Caught ArithmeticException: " + e);
}
// Handle ArrayIndexOutOfBoundsException
try {
nums[5] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught ArrayIndexOutOfBoundsException: " + e);
}
// Handle NullPointerException
try {
String str = null;
System.out.println(str.length());
} catch (NullPointerException e) {
System.out.println("Caught NullPointerException: " + e);
}
System.out.println("After all try-catch blocks.");
}
}