Test -1
83
-----------
Given: public static void main(String[] args){ try(FileInputStream fis = new FileInputStream("c:\\temp\\test.text")){ System.out.println(fis); } //line 1 }
Which of the following options can be inserted at //1 independent of each other to make the above code compile? Note that FileInputStream, try-with-resources, and multi-catch are NOT mentioned in the official exam objectives. However, we have seen questions in the exam that require knowledge on these topics. It is possible that these are questions are unscored i.e. their score doesn't count towards your final score. Since we have no way of knowing that, it is better to read up on these topics.
catch(IOException){ } ; This was specified as correct answer; but there is not identifier; like IOExceptio ioe;
This wont compile. Thats why i did not select this.
catch(IOException e){ } finally{ fis.close(); } -> This seems better option than the before one. But this option is wrong as per Enthuware.
About Question enthuware.ocpjp.i.v11.2.3142 :
Moderator: admin
-
- Posts: 3
- Joined: Fri Jul 10, 2020 7:30 pm
- Contact:
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3142 :
You are right. It should have the parameter name. Fixed.
The last option invalid because the close() method on FileInputStream declares that it may throw IOException. So, the exception from the call to fis.close(); remains unhandled.
The last option invalid because the close() method on FileInputStream declares that it may throw IOException. So, the exception from the call to fis.close(); remains unhandled.
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Thu Nov 21, 2019 5:48 am
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3142 :
I think it will not compile because the variable fis is not accessible from within the finally block. The same is with the first option.The last option invalid because the close() method on FileInputStream declares that it may throw IOException. So, the exception from the call to fis.close(); remains unhandled.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3142 :
You are right. Fixed.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Fri Apr 08, 2022 10:34 am
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3142 :
Question for the first option
Explanation:
The IOException thrown from the try clause is not being caught and is not declared in the throws clause either. Further, since the close() method also throws IOException, that exception also remains unhandled. Therefore, this code won't compile.
I tried this code in intellij and it was saying the new FileInputStream() is throwing the FIleNotFoundException. I got confused at first because the explanation said that the IOException in the try clause is the one that's not declared. However, when I tried to add a catch for IOException, the error was gone. I understand FileNotFoundException is a subclass of IOException which is why it worked. I just want to ask if the explanation should've specifically mention both FileNotFoundException & IOException was not being caught?
Or is this dependent on the java version used(mine is 11) that's why only IOException was mentioned?
Code: Select all
finally{ tempFis.close(); }
The IOException thrown from the try clause is not being caught and is not declared in the throws clause either. Further, since the close() method also throws IOException, that exception also remains unhandled. Therefore, this code won't compile.
I tried this code in intellij and it was saying the new FileInputStream() is throwing the FIleNotFoundException. I got confused at first because the explanation said that the IOException in the try clause is the one that's not declared. However, when I tried to add a catch for IOException, the error was gone. I understand FileNotFoundException is a subclass of IOException which is why it worked. I just want to ask if the explanation should've specifically mention both FileNotFoundException & IOException was not being caught?
Or is this dependent on the java version used(mine is 11) that's why only IOException was mentioned?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.i.v11.2.3142 :
Your confusion is valid. The explanation should say FileNotFoundException instead of IOException because that is what FileInputStream constructor says in its throws clause. The explanation says IOException loosely because all file i/o related exceptions extends from IOException. So, even though catching IOException is ok (it gets rid of the error) it is imprecise and should have been avoided.
Further, the close method throws IOException and since it is a super class of FileNotFoundException, if you handle IOException, FileNotFoundException is automatically taken care of. But yes, the explanation should make this clear.
thank you for your feedback!
Paul.
Further, the close method throws IOException and since it is a super class of FileNotFoundException, if you handle IOException, FileNotFoundException is automatically taken care of. But yes, the explanation should make this clear.
thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 14 guests