Question: import java.io.*; public class TestExceptions{ public static void main(String[] args){ //Open the Input file File inputFile = new File(Sample.jpg); //Open the Output file File outputFile1
![import java.io.*; public class TestExceptions{ public static void main(String[] args){ //Open](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f3b05d6dc94_42866f3b05cd9ea7.jpg)
import java.io.*; public class TestExceptions{ public static void main(String[] args){ //Open the Input file File inputFile = new File("Sample.jpg"); //Open the Output file File outputFile1 = new File("Output1.jpg");
//Get file handlers in Byte Stream format FileInputStream in1 = new FileInputStream(inputFile); FileOutputStream out1 = new FileOutputStream(outputFile1);
int c1;
while ((c1 = in1.read()) != -1){ //Until end of file out1.write(c1); }
//Close the files in1.close(); out1.close(); } }
Simple exception handling a. Download the following file from the class website: TestExceptions.java b. Compile the code and notice the compilation error message. c. Modify the main method to propagate the IOException this is an ancestor of FileNot FoundException). This is done using a throws clause when the method is first declared. d. While there are multiple exceptions that might be thrown by this code, this change should propagate all of them, since they all inherit from IOException. e. Execute the program and notice the run-time error. f. Modify the code to handle the exceptions using a try-catch block instead of propagating the exception. When the exception occurs print: "The file you have requested cannot be found." g. Test your changes
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
