Page 1 of 1

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

Posted: Wed Aug 12, 2020 8:37 pm
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?

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

Posted: Wed Aug 12, 2020 11:40 pm
by admin
What happened when you compiled and ran the code?

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

Posted: Mon Nov 02, 2020 10:36 pm
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.

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

Posted: Mon Nov 02, 2020 11:16 pm
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.

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

Posted: Tue Jan 18, 2022 3:19 am
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.

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

Posted: Tue Jan 18, 2022 6:54 am
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.