Java Lab 11
Java Lab 11
import java.util.Scanner;
try {
// Get user input for Num1
System.out.print("Enter Num1: ");
int num1 = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
// Handle NumberFormatException
System.out.println("NumberFormatException: Please enter valid integers.");
} catch (ArithmeticException e) {
// Handle ArithmeticException
System.out.println("ArithmeticException: Division by zero is not allowed.");
} finally {
// Close the scanner
scanner.close();
}
}
import java.util.Scanner;
try {
// Get user input for the index to access
System.out.print("Enter the index to access (0 to " + (numbers.length - 1) + "): ");
int index = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
// Handle NumberFormatException
System.out.println("NumberFormatException: Please enter a valid integer.");
} catch (ArrayIndexOutOfBoundsException e) {
// Handle ArrayIndexOutOfBoundsException
System.out.println("ArrayIndexOutOfBoundsException: Index is out of bounds.");
} catch (Exception e) {
// Handle other exceptions
System.out.println("Exception: " + e.getMessage());
} finally {
// Close the scanner
scanner.close();
}
}
}