2. Write a program named CommonExceptionDemo.java to accomplish the following tasks: (1) Displays the info(name and ordi
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
2. Write a program named CommonExceptionDemo.java to accomplish the following tasks: (1) Displays the info(name and ordi
import java.util. Input MismatchException: import java.util.Scanner: en Exceptionnum ARITHMETIC INDEXOUTOFBOUNDS, NEGATIVEARRAYSIZE, NULLPOINTER, NUMBER FORMAT CLASSCAST: private Exceptione: public Exception getException ) { return e; Complete codes for the method public static ExceptionEnum getExceptionByordinal (int ord) throws IllegalargunentException ! //Complete code here. This method returns the element whone ordinal value is the same with ord //and throw an legalArgumentException to alert the value of ord is not in the range of 0.5 1 Hint: Using switch-case to check which status matches the ord value. In each case, new an exception object(or instance) associated with the exceptionEnum, and return one of the ExceptionEnum of the enumerated type. Otherwise, throws an illegalArgumentExpection. public class CommonExceptionDemo public static void main(string] args) { 1/complete code in main method to show your running result like output sample
Output sample: Exception: ARITHMETIC(O) The program has been INDEXOUTOFBOUNOS(1) run for three times. NEGATIVEARRAYSIZE (2) NULLPOINTER(3) When you create an ArithmeticException NUMBERFORMAT (4) CLASSCAST(5) object, please invoke its constructor with Exception: argument "/ by zero" ARITHETIC(O) Please input an integer to select the TYPE OF EXCEPTION(-5):0 INDEXOUTOFBOUNDS (1) java.lang.ArithmeticException by zero NEGATIVEARRAYSIZE(2) at Exceptionnum.getExceptiondyOrdinal (Common xceptionDemova:21) NULLPOINTER(3) at CommonExceptionDeo, main(Commonixceptionem. 12:56) NUMBERFORMAT(4) CLASSCAST (5) Plesse Input an Integer to select the TYPE OF EXCEPTION(6-5) java.util. Inutiseatchboxception Here is the End Exception ARITHMETIC(O) INDEXOUTOFBOUNDS (1) NEGATIVEARRAYSIZE(2) WOLLPOINTER(3) NUMBER FORT(4) CLASSCAST (5) Please input an integer to select the TYPE OF EXCEPTION(O) 17 java.lang. IllegalArgumentException Here is the End