Lab Evaluation 05
Lab Evaluation 05
import java.util.Scanner;
int a = scanner.nextInt();
int b = scanner.nextInt();
try {
int result = a / b;
} catch (ArithmeticException e) {
scanner.close();
Problem 2:
import java.util.Scanner;
try {
System.out.println("Element at index " + index + " is " + array[index]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index out of bounds.");
}
scanner.close();
}
}
Problem 3:
import java.util.Scanner;
try {
System.out.print("Enter a number to divide 100 by: ");
int divisor = scanner.nextInt();
int result = 100 / divisor;
System.out.println("Division result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Array index out of bounds.");
} catch (NumberFormatException e) {
System.out.println("Error: Invalid number format.");
}
scanner.close();
}
}