Write a program named CommonExceptionDemo.java to accomplish the following tasks: (1) Displays the info(name and ordinal value) of every element in an enum ExceptionEnum. (2) Asks the user to input a integer. (3) According to the value of user’s input, triggers the Exception ⚫ While getting the input value, uses try-catch to check. ⚫ If the input is a number and its value is between 0 and 5,triggers the Exception in ExceptionEnum and shows its information. ⚫ If the input is a number but its value is not between 0 and 5, throws an IllegalArgumentException, catches it and prints the exception message. ⚫ If the input is not a number, triggers InputMismatchException, catches it and prints the exception message.
时间: 2023-11-27 20:52:11 浏览: 97
Here's the program CommonExceptionDemo.java that satisfies the given requirements:
```java
import java.util.InputMismatchException;
import java.util.Scanner;
enum ExceptionEnum {
EXCEPTION_ONE("Exception One", 1),
EXCEPTION_TWO("Exception Two", 2),
EXCEPTION_THREE("Exception Three", 3),
EXCEPTION_FOUR("Exception Four", 4),
EXCEPTION_FIVE("Exception Five", 5);
private String name;
private int ordinalValue;
ExceptionEnum(String name, int ordinalValue) {
this.name = name;
this.ordinalValue = ordinalValue;
}
public String getName() {
return name;
}
public int getOrdinalValue() {
return ordinalValue;
}
}
public class CommonExceptionDemo {
public static void main(String[] args) {
displayEnumElements();
Scanner scanner = new Scanner(System.in);
int input = 0;
try {
System.out.print("Enter a number between 0 and 5: ");
input = scanner.nextInt();
if (input < 0 || input > 5) {
throw new IllegalArgumentException("Input value is not between 0 and 5");
}
ExceptionEnum exception = ExceptionEnum.values()[input];
throw new Exception(exception.getName());
} catch (InputMismatchException e) {
System.out.println("InputMismatchException: " + e.getMessage());
} catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException: " + e.getMessage());
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
private static void displayEnumElements() {
for (ExceptionEnum exception : ExceptionEnum.values()) {
System.out.println(exception.getName() + " - " + exception.getOrdinalValue());
}
}
}
```
Explanation:
- We define an enum `ExceptionEnum` with 5 constants, each having a name and an ordinal value.
- We define a class `CommonExceptionDemo` with a `main` method that displays the info(name and ordinal value) of every element in `ExceptionEnum`.
- We use a `Scanner` object to take input from the user and wrap it inside a try-catch block to handle any potential `InputMismatchException`.
- If the input is a valid number between 0 and 5, we retrieve the corresponding `ExceptionEnum` constant and throw a new `Exception` with its name.
- If the input is not a number or its value is not between 0 and 5, we throw an `IllegalArgumentException` with a specific error message.
- We catch the exceptions thrown above and print their respective error messages.
Sample Output:
```
Exception One - 1
Exception Two - 2
Exception Three - 3
Exception Four - 4
Exception Five - 5
Enter a number between 0 and 5: 2
Exception: Exception Three
```
```
Exception One - 1
Exception Two - 2
Exception Three - 3
Exception Four - 4
Exception Five - 5
Enter a number between 0 and 5: -1
IllegalArgumentException: Input value is not between 0 and 5
```
```
Exception One - 1
Exception Two - 2
Exception Three - 3
Exception Four - 4
Exception Five - 5
Enter a number between 0 and 5: abc
InputMismatchException: For input string: "abc"
```
阅读全文