About Question enthuware.ocpjp.v7.2.1205

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
sasha32003
Posts: 12
Joined: Tue Aug 05, 2014 2:47 am
Contact:

About Question enthuware.ocpjp.v7.2.1205

Post by sasha32003 »

Consider the following code:

Code: Select all

public class FileCopier {
     public static void copy(String records1, String records2)  {
         try {InputStream is = new FileInputStream(records1);
             OutputStream os = new FileOutputStream(records2); 
            byte[] buffer = new byte[1024];
             int bytesRead = 0;
             while ((bytesRead = is.read(buffer)) != -1) { 
                os.write(buffer, 0, bytesRead);
                 System.out.println("Read and written bytes " + bytesRead);
             } 
        } catch (FileNotFoundException | IndexOutOfBoundsException e) {             e.printStackTrace();         }     }     

 public static void main(String[] args) {
         copy("c:\\temp\\test1.txt", "c:\\temp\\test2.txt");
  } 
}


Assuming appropriate import statements and the existence of both the files, what will happen when the program is compiled and run?

The problem is. I am not agree with explanation for the right answer.

Answer is this: The program will not compile because it does not handle exceptions correctly.
Explanation: Remember that most of the I/O operations (such as opening a stream on a file, reading or writing from/to a file) throw IOException and this code does not handle IOException. FileNotFoundException is a subclass of IOException and IndexOutOfBoundsException is subclass of RuntimeException. The code can be fixed by replacing both the exceptions with IOException.

The first part is fine, catch clause doesn't handle the IOException, so by replacing just FileNotFoundException with IOException will compile and run just fine! Why do we need to replace BOTH exceptions with IOException? and not just first one? More ... if we replace BOTH the exceptions with IOException the code will not compile.

Please correct me if it was misunderstanding.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1205

Post by admin »

It is trying to say that replace both of them together i.e. the whole expression with just one IOException.
You approach is correct as well.
If you like our products and services, please help us by posting your review here.

sasha32003
Posts: 12
Joined: Tue Aug 05, 2014 2:47 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1205

Post by sasha32003 »

admin wrote:It is trying to say that replace both of them together i.e. the whole expression with just one IOException.
You approach is correct as well.
Ok, thanks ... clear now.

goodness
Posts: 2
Joined: Mon Nov 10, 2014 6:14 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1205

Post by goodness »

Another way to correct is to specify the Exceptions Thrown by a Method

Add a throws clause to the method declaration for the copy method to specify that it can throw IOException.
Example:
public static void copy(String records1, String records2) throws IOException

Therefore, when IOException is throw inside the copy method, a method further up the call stack will handle the IOException.

Code: Select all

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileCopier {
    public static void copy(String records1, String records2) throws IOException {
        try(InputStream is = new FileInputStream(records1);
            OutputStream os = new FileOutputStream(records2);) {
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = is.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
                System.out.println("Read and written bytes " + bytesRead);
            }
        } catch (FileNotFoundException | IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) throws Exception {
        copy("c:\\temp\\test1.txt", "c:\\temp\\test2.txt");
    }
}

EelcoD
Posts: 10
Joined: Sat Jan 24, 2015 8:09 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1205

Post by EelcoD »

I'm sorry to disagree with the answer:
The program will not compile because it does not handle exceptions correctly.

It does HANDLE correctly, because all it does is: e.printStackTrace();
That's the handling!

The catch clause, and that's what is between the brackets:
catch (FileNotFoundException | IndexOutOfBoundsException e)
is used incorrectly.

Anyone back me up please? Or am I being stupid here...

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1205

Post by admin »

"Exception handling" is a generic term that means declaring the throws clause and/or wrapping the code in appropriate try/catch clause. e.printStackTrace is NOT "exception handling".
If you like our products and services, please help us by posting your review here.

EelcoD
Posts: 10
Joined: Sat Jan 24, 2015 8:09 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1205

Post by EelcoD »

That's why I say I'm sorry to disagree.
I still disagree, but that's my problem.

iivanovic
Posts: 2
Joined: Tue May 12, 2020 9:17 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1205

Post by iivanovic »

I also agree that catch clause used incorectly, and that official answer is not correct. As I see:
try {
} catch (<this is catch clause>) {
<there we handle caught exception>
}
Where we should correct code to compile correctly?

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1205

Post by admin »

Why do you think the catch clause is incorrect? You could as easily fix it with a throws clause in the method.
That's the point. It is neither the catch clause nor the throws clause in itself that is the problem here. "Exception handling" does not just mean a catch clause or just the throws clause. It implies the appropriate and coordinated usage of both depending on the code that is there in the method.

The given code handles only the two class of exceptions (mentioned in the catch clause) correctly, but it still doesn't get the whole thing right. Otherwise the code would have compiled! The code in the method throws IOException. What happens to that? So, the only reasonable thing to say here is that the whole setup of the given code is incorrect. It doesn't handle exceptions correctly.
If you like our products and services, please help us by posting your review here.

iivanovic
Posts: 2
Joined: Tue May 12, 2020 9:17 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1205

Post by iivanovic »

I am ok with this: "Exception handling" does not just mean a catch clause or just the throws clause.
Anyway, when I test given code I found out that code compiles correctly when catch clause "FileNotFoundException | IndexOutOfBoundsException e" is replaced with: "IOException e". I only thought that answer: "The program will not compile because the catch clause is used incorrectly." better explains what is wrong with code. So officially correct answer totally confused me. Hope real exam contains less as possible questions like this.

Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests