About Question enthuware.ocpjp.v7.2.1274 :

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

Moderator: admin

Post Reply
alexz1011
Posts: 4
Joined: Sun Mar 29, 2015 7:20 am
Contact:

About Question enthuware.ocpjp.v7.2.1274 :

Post by alexz1011 »

Hi,

I understand

Code: Select all

IOException|RuntimeException
works, but can you please explain why the answer is not

Code: Select all

FileNotFoundException|IOException
?

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

Re: About Question enthuware.ocpjp.v7.2.1274 :

Post by admin »

FileNotFoundException is a subclass of IOException. You cannot include classes that are related by inheritance in the same multi-catch block.

dvc1190
Posts: 15
Joined: Tue Feb 25, 2014 3:14 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1274 :

Post by dvc1190 »

The reason why the following answer is incorrect isn't really explained:
FileNotFoundException|SecurityException|IllegalArgumentException

I think it's because one or more of the exceptions is not thrown by the code in the try block, but I'm not sure.

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

Re: About Question enthuware.ocpjp.v7.2.1274 :

Post by admin »

It does explain it. I see the following explanation under this option:
Note that most commonly used methods in Java API that deal with reading or writing files have java.io.IOException in their throws clause. So you must handle this exception. At run time, more specific exceptions such as FileNotFoundException are actually thrown depending on the actual cause of the problem.

dongyingname
Posts: 18
Joined: Sat Jun 22, 2019 4:10 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1274 :

Post by dongyingname »

The reason why the following answer is incorrect isn't really explained:
FileNotFoundException|SecurityException|IllegalArgumentException

I think it's because one or more of the exceptions is not thrown by the code in the try block, but I'm not sure.
I think the another way to explain would be:
FileNotFoundException, SecurityException, and IllegalArgumentException may be the actual exceptions the application is throwing at run time, the compiler only requires you to handle those, which are obviously thrown by the methods in the try-block. And the compiler will complain about those exceptions, which you declare in the catch argument and it cannot see, for example SecurityException.

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests