WAP using try catch block. User should enter two command line arguments. If only one argument is entered then exception should be caught. In case of two command line arguments, if first is divided by second and if second command line argument is 0 then catch the appropriate exception
public class Main {
public static void main(String[] args) {
try {
// Check if two command line arguments are provided
if (args.length != 2) {
throw new IllegalArgumentException("Please provide two command line arguments.");
}
// Parse command line arguments
int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);
// Check if the second argument is 0
if (num2 == 0) {
throw new ArithmeticException("Cannot divide by zero.");
}
// Divide the first argument by the second argument
int result = num1 / num2;
System.out.println("Result of division: " + result);
} catch (NumberFormatException e) {
System.err.println("Please enter valid integer arguments.");
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
} catch (ArithmeticException e) {
System.err.println(e.getMessage());
}
}
}