Question
import java.io.*; public class ExceptionTesterApp { public static void main(String[] args) { System.err.println(In main: calling Method1.); Method1(); System.err.println(In main: returned from Method1.); } public static
import java.io.*;
public class ExceptionTesterApp { public static void main(String[] args) { System.err.println("In main: calling Method1."); Method1(); System.err.println("In main: returned from Method1."); }
public static void Method1() { System.err.println("\tIn Method1: calling Method2."); Method2(); System.err.println("\tIn Method1: returned from Method2."); }
public static void Method2() { System.err.println("\t\tIn Method2: calling Method3."); Method3(); System.err.println("\t\tIn Method2: returned from Method3."); }
public static void Method3() { System.err.println("\t\t\tIn Method3: Entering.");
//Add code to throw an exception here.
System.err.println("\t\t\tIn Method3: Exiting."); } }
Add code to Method3 that throws an unchecked exception by attempting to divide an integer by zero. Complie and run the program and note where the exception is thrown.
Delete the code you just added to Method3. then, add a statement to this method like the (error: unreported exeption IOExeption; must be caught or declared to be thrown getRecordCount(). ) That creates an object from the RandomAccessFile class, but use the string "product.ran" in place of the path variable. The construct for this class throws a checked exeption named FileNotFoundExeption. Note the error message that indicates that you haven't handled the exeption. If this error message isn't shown, compile the class to display the error message.
Add throw clauses to all of the methods including the main method. Then, run the program to see how a checked exeption can propagate all the way out of a program.
Add the code necessary to handle the FileNotFoundExeption in Method1. To do that, you'll need to remove the throws clauses from the main method and Method1, and you'll need to add a try statement to Method1 that catches the exeption. The catch block should display an appropriate error message. Run the program to make sure the exeption handler works.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started