JAVA, PLEASE EXPLAIN ALL STEP. Assume that processor refers to an object that pr
ID: 3603499 • Letter: J
Question
JAVA, PLEASE EXPLAIN ALL STEP.
Assume that processor refers to an object that provides a void method named process that takes no arguments. As it happens, the process method may throw one of several exceptions. One of these is NumberFormatException, another is FileNotFoundException. And there are others. Write some code that invokes the process method provided by the object associated with processor and arranges matters so that if a NumberFormatException is thrown, the message "Bad Data" is printed to standard output, and if FileNotFoundException. is thrown, the message "Data File Missing" is printed to standard output. If some other Exception is thrown, its associated message is printed out to standard output.
Explanation / Answer
package lab7;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class CustomExceptionmessages {
void process() {
String str = null;
try {
System.out.println(
"Please select exception type 1.FileNotFoundException 2.NumberFormatException 3.ArithmeticException 4.ArrayIndexOutOfBounds 5.StringIndexOutOfBound 6.NullPointerException 7.Other Exception");
Scanner sc = new Scanner(new InputStreamReader(System.in));
int input = sc.nextInt();
switch (input) {
case 1:
FileInputStream fis = new FileInputStream("abc.txt");
break;
case 2:
int num = Integer.parseInt("XYZ");
break;
case 3:
int i = 23 / 0;
break;
case 4:
int a[] = new int[10];
a[100] = 9;
break;
case 5:
str = "Hello";
char c = str.charAt(40);
break;
case 6:
str = null;
System.out.println(str.length());
break;
default:
}
sc.close();
} catch (FileNotFoundException fnfe) {
System.out.println("Data File Missing");
} catch (NumberFormatException nfe) {
System.out.println("Bad Data");
}
catch (Exception e) {
System.out.println(e.getCause());
}
}
public static void main(String args[]) {
CustomExceptionmessages c = new CustomExceptionmessages();
c.process();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.