write a java program that handles divide by zero and array out of bound exception using…
- User
write a java program that handles divide by zero and array out of bound exception using multiple catch
- Assistant
write a java program that handles divide by zero and array out of bound exception using multiple catch
Certainly! Handling exceptions effectively is crucial for building robust Java applications. Below is a Java program that demonstrates how to handle both ArithmeticException (which occurs during a divide by zero) and ArrayIndexOutOfBoundsException using multiple catch blocks.
Divide by Zero Scenario:
ArithmeticException is thrown.Array Index Out of Bounds Scenario:
ArrayIndexOutOfBoundsException is thrown.Exception Handling:
catch block.finally block is used to execute code regardless of whether an exception occurred.import java.util.Scanner;
public class ExceptionHandlingDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Example 1: Handling Divide by Zero
try {
System.out.println("=== Divide by Zero Example ===");
System.out.print("Enter dividend (integer): ");
int dividend = scanner.nextInt();
System.out.print("Enter divisor (integer): ");
int divisor = scanner.nextInt();
int result = dividend / divisor; // May throw ArithmeticException
System.out.println("Result: " + result);
} catch (ArithmeticException ae) {
System.out.println("Error: Cannot divide by zero!");
}
// Example 2: Handling Array Index Out of Bounds
try {
System.out.println("\n=== Array Index Out of Bounds Example ===");
int[] numbers = {10, 20, 30, 40, 50};
System.out.print("Enter array index to access (0-4): ");
int index = scanner.nextInt();
System.out.println("Element at index " + index + " is " + numbers[index]); // May throw ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException aioobe) {
System.out.println("Error: Invalid array index!");
}
// Optional: Additional code that always runs
finally {
System.out.println("\nProgram execution completed.");
scanner.close();
}
}
}
Import Statement:
import java.util.Scanner; is used to take input from the user.Main Class and Method:
ExceptionHandlingDemo class contains the main method, which is the entry point of the program.Divide by Zero Handling:
dividend / divisor is performed.ArithmeticException is thrown and caught by the corresponding catch block, which prints an error message.Array Index Out of Bounds Handling:
numbers is initialized with five elements.numbers[index] may throw an ArrayIndexOutOfBoundsException if the index is invalid, which is caught and handled by the respective catch block.Finally Block:
finally block contains code that will execute regardless of whether an exception was thrown or not. In this case, it prints a completion message and closes the Scanner resource to prevent resource leaks.=== Divide by Zero Example ===
Enter dividend (integer): 100
Enter divisor (integer): 0
Error: Cannot divide by zero!
=== Array Index Out of Bounds Example ===
Enter array index to access (0-4): 5
Error: Invalid array index!
Program execution completed.
Multiple catch Blocks:
catch blocks to handle different types of exceptions separately.catch block should handle a specific exception type.Order of catch Blocks:
finally Block:
finally block is useful for executing code that must run regardless of whether an exception was thrown, such as closing resources.Best Practices:
catch blocks as they can make debugging difficult.This program provides a clear example of how to handle specific exceptions in Java using multiple catch blocks, enhancing the program's robustness and user experience.
Not yet included in a published release.