Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Creating A Database In Filemaker Pro Visual QuickProject Guide

Authors: Steven A. Schwartz

1st Edition

0321321219, 978-0321321213

More Books

Students also viewed these Databases questions