• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

checked exception

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from KS && BB master exam:
Ans: The code fails to compile due to an error on line no.13. (But the code is working fine). The explanation given is you can't use a method that throws a checked exception in catch or finally blocks, such methods should be in try blocks.
Please let me know what should I conclude now..?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the question asked there?
Its because the close method throws an IOException. Also its always good to check that the reference is not null.
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohamed sanaullah wrote:What is the question asked there?
Its because the close method throws an IOException. Also its always good to check that the reference is not null.



Question is asked as: Given that all the methods of FileInputStream throws an IOException, then which of the following is true(among given options):

And the answer is: The program fails to to compile due to an error at line 13.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:

mohamed sanaullah wrote:What is the question asked there?
Its because the close method throws an IOException. Also its always good to check that the reference is not null.



Question is asked as: Given that all the methods of FileInputStream throws an IOException, then which of the following is true(among given options):

And the answer is: The program fails to to compile due to an error at line 13.



Dear swaraj gupta, If you take a look to your main() method you will find it declare that it throw IOException beside the try/catch block in the method block. If you omit that throws clause in the main() method signature you will sure get a compile time exception because of using fis.close() in the finally block without nesting it in a try/catch block
But the code as you upload it will compile without errors.

regards
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Imad Aydarooos wrote:Dear swaraj gupta, If you take a look to your main() method you will find it declare that it throw IOException beside the try/catch block in the method block. If you omit that throws clause in the main() method signature you will sure get a compile time exception because of using fis.close() in the finally block without nesting it in a try/catch block
But the code as you upload it will compile without errors.

regards



Thanks Imad..got the point..
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic