87.6k views
3 votes
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.

User Jessiica
by
5.3k points

1 Answer

2 votes

Answer:

try {

processor.process();

} catch (NumberFormatException nfe) {

System.out.println("Bad Data");

} catch (FileNotFoundException fnfe) {

System.out.println("Data File Missing");

} catch (Exception ex) {

System.out.println(ex.getMessage());

}

Step-by-step explanation:

The first statement invokes the process() method. Here process() is the method name and processor is the name of the object.

try catch statement here is used to catch given exceptions in different catch blocks. When any of these exceptions occur in try block, the corresponding catch block is executed. This catch block is used to handle that particular exception.

In the above chunk of code, if the exception NumberFormatException is thrown then the message "Bad Data" is displayed in output. The System.out.println() is used to display the message on the output screen. Similarly when the exception FileNotFoundException is thrown the message "Data File Missing" is printed. The last catch Exception is used when any other exception is thrown and the getMessage() method is called to get the associated message and System.out.println here is used to print that associated message according to the thrown exception.

User Ilie Pandia
by
5.8k points