Question: import java.io.*; public class TestFinally{ public static void main(String[] args){ FileInputStream in1 = null; FileInputStream in2 = null; //Open an existing file trycatch.txt File inputFile1

 import java.io.*; public class TestFinally{ public static void main(String[] args){ FileInputStream

import java.io.*;

public class TestFinally{ public static void main(String[] args){ FileInputStream in1 = null; FileInputStream in2 = null;

//Open an existing file trycatch.txt File inputFile1 = new File("trycatch.txt"); //Open a non-existent file nosuchfile.abc File inputFile2 = new File("nosuchfile.abc");

//1. Add the try statement here

//Get file handlers in Byte Stream format in1 = new FileInputStream(inputFile1); in2 = new FileInputStream(inputFile2);

int c1;

//Try to read 'nosuchfile.abc' till the end of File while ((c1 = in2.read()) != -1){ System.out.println("Read from nosuchfile.abc"); }

//2. Close the try block here //3. Add a catch block containing meaningful error messages.

//4. Add the finally block here. See instructions in the handout.

//Close the files

in1.close(); System.out.println("Closing file 'trycatch.txt' inside finally block."); in2.close(); System.out.println("Closing file 'nosuchfile.abc' inside finally block.");

//5. Close the finally block here. } }

Examine the code to determine what it does Compile the code and notice the error. Modify TestFinally.java to handle the exception following these instructions (and those embedded in the code) i. Embedded in the code is a note showing the location for the try statement. i. The first line of the catch block should look like this: catch(IOException e) Display a meaningful error message to indicate the error that has occurred lll. and include the following statement in your catch clause: System out println("The exception is e); iv. Even when an error occurs in a method, there may be clean-up activities that are required before the method or program terminates. In this case, any open files should be closed prior to terminating the program. v. Include a finally block that closes the files using notes 4 and 5 in the code that show where to place the beginning and end of the block vi. You will need to add a try-catch block inside the finally block to handle any IOExceptions associated with the file close operation Test your code. When it is working, you will generate another type of exception Add a second catch clause inside the finally block with a meaningful message to handle this new exception

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!