About Question enthuware.ocpjp.ii.v11.2.1202 :

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

Moderator: admin

Post Reply
csgear
Posts: 3
Joined: Fri Jul 24, 2020 12:22 pm
Contact:

About Question enthuware.ocpjp.ii.v11.2.1202 :

Post by csgear »

for this question, I am wondering which class throws java.io.InvalidClassException?


//Assume appropriate imports
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);
}

} catch (FileNotFoundException | java.io.InvalidClassException e) {
e.printStackTrace();
}
}

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

Given that test1.txt exists but test2.txt does not exist, what will happen when the above program is compiled and run?

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

Re: About Question enthuware.ocpjp.ii.v11.2.1202 :

Post by admin »

What happened when you compiled and ran the code?
If you like our products and services, please help us by posting your review here.

olsongrant
Posts: 8
Joined: Mon Nov 02, 2020 10:21 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.1202 :

Post by olsongrant »

I was surprised that the compiler allowed this InvalidClassException, as well. InvalidClassException seems to relate to object serialization, and that's not what the code is doing. I did try catching java.text.ParseException, just to check my sanity that the compiler would complain about a catch block that referred to a checked exception that could not be thrown by the methods used in the block. And yes, the compiler did complain about the ParseException.

I think, and I'm making a possibly hasty conclusion here, that the compiler would allow the catching of any subclass of IOException here because the code declares that it throws IOException. InvalidClassException is a descendant of IOException. I tried javax.net.ssl.SSLException, to test my theory, and it worked as well.

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

Re: About Question enthuware.ocpjp.ii.v11.2.1202 :

Post by admin »

That's right. Since the method declaration says it throws IOException, you can have a catch block that tries to catch any subclass of IOException.
If you like our products and services, please help us by posting your review here.

sijucm
Posts: 8
Joined: Tue Jan 11, 2022 4:41 am

Re: About Question enthuware.ocpjp.ii.v11.2.1202 :

Post by sijucm »

Nice trick. Is that what Oracle people also do? These questions are meant to make people who know how it works get it wrong.
If you know you cannot catch a checked exception not actually thrown, then you will see the InvalidClassException so brightly shown in the middle of the screen and mark it as "does not compile". Please also note there is a time limit to answer. And then there is an IOException throws from the main method, which you will not notice and also the fact that if it is thrown it is allowed to catch.
I hope, you can change these questions if it is not really the exam type of question. There is no end.

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

Re: About Question enthuware.ocpjp.ii.v11.2.1202 :

Post by admin »

Hi Sijucm,
Our job is to prepare you for the exam. We we get into the argument of what is and what is not useful, we will not be doing that job well. But I understand your concern and rest assured that you are not alone. Many people have the same complaint. The right forum to raise your concern might be twitter or some other social media network.
Here, we try to keep the discussion focused on the task at hand, i.e., learning what we need to learn for passing the exam.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 41 guests