Question: In Java pls, please include comments pls, thank you will up vote TestExceptions.java///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import java.io.*; public class TestExceptions{ public static void main(String[] args){ //Open the

In Java pls, please include comments pls, thank you will up vote

In Java pls, please include comments pls, thank you will up vote

TestExceptions.java/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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(); } }

CallStack/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class CallStack{ // Called by func1() void func2 (){ System.out.println("In func2 method"); int a = 0; int b; b = 10 / a; } //Called by Main void func1(){ System.out.println("In func1 method"); this.func2 (); System.out.println("Back in func1 method"); } public static void main (String args[]){ CallStack myCallStack; myCallStack = new CallStack(); System.out.println("In the main method"); myCallStack.func1 (); } }

TestFinally/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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. } }

Summary In this part of the lab, you will fix errors and modify the code to handle exceptions for three different cases below. 1. 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 FileNotFoundException). 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. 2. Handling exceptions a. Download the following file from the class website: CallStack.java b. Examine the code to determine what it does. c. Compile and execute the code. d. Modify the main() method to handle the exception that is propagated to it. Use a try-catch block to display a meaningful error message when the exception occurs. e. Test your code. Notice that, although the exception was thrown in func2, it is caught by the catch block in the main method. 3. Handling exceptions with a finally clause a. Download the following file from the class website: TestFinally.java b. Examine the code to determine what it does. c. Compile the code and notice the error. d. 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. ii. The first line of the catch block should look like this: catch(IOException e) { iii. Display a meaningful error message to indicate the error that has occurred 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 e. Test your code. When it is working, you will generate another type of exception. f. Add a second catch clause inside the finally block with a meaningful message to handle this new exception. Summary In this part of the lab, you will fix errors and modify the code to handle exceptions for three different cases below. 1. 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 FileNotFoundException). 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. 2. Handling exceptions a. Download the following file from the class website: CallStack.java b. Examine the code to determine what it does. c. Compile and execute the code. d. Modify the main() method to handle the exception that is propagated to it. Use a try-catch block to display a meaningful error message when the exception occurs. e. Test your code. Notice that, although the exception was thrown in func2, it is caught by the catch block in the main method. 3. Handling exceptions with a finally clause a. Download the following file from the class website: TestFinally.java b. Examine the code to determine what it does. c. Compile the code and notice the error. d. 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. ii. The first line of the catch block should look like this: catch(IOException e) { iii. Display a meaningful error message to indicate the error that has occurred 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 e. Test your code. When it is working, you will generate another type of exception. f. 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 Finance Questions!