Lab5 Vivaan 81
Lab5 Vivaan 81
Scanner;
try {
if (denominator == 0) {
throw new ArithmeticException("Denominator cannot be zero.");
}
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
2.import java.util.Scanner;
public class Lab52 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.println("Element at index " + index + ": " + array[index]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Invalid index. " + e.getMessage());
}
}
}
3.import java.util.Scanner;
try {
System.out.print("Enter the first number: ");
String input1 = scanner.nextLine();
int num1 = Integer.parseInt(input1);
} catch (NumberFormatException e) {
System.out.println("Error: Invalid input. Please enter valid integers.");
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
}
}
}
try {
System.out.println("Length of the string: " + str.length());
} catch (NullPointerException e) {
System.out.println("Error: NullPointerException - Cannot call method on a null object.");
}
}
}
5.import java.util.Scanner;
import java.util.InputMismatchException;
6.import java.util.Scanner;
try {
System.out.print("Enter your age: ");
int age = scanner.nextInt();
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
}
}