• 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

help compiling

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've written this code but it's not compiling. The error message is saying 'unreported exception java.io FileNotFoundException; must be caught or declared to be thrown.
I've only just learnt about creating your own methods and i'm struggling with it a bit, but i think what i've done is right.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The error message is saying 'unreported exception java.io FileNotFoundException; must be caught or declared to be thrown.



In Java, certain methods are defined to throw checked exception conditions. These are exceptions that occur, that you can't just ignore -- you either have to deal with them yourself (using try-catch clause) or you have to declare that they are not handled (and that your method can throw those exceptions). The file not found exception is one of those exceptions -- either you must handle it or your method must be declared to throw it.

Henry
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Surround the block of code which instantiates and uses a File Object with a try-catch block as follows -



As also mentioned by Henry, the other way of taking care of checked exceptions is to throw them from the method instead of handling them. So, you can have the controlMenu throw the Exception as follows -



If you go ahead with this approach, methods which invoke controlMenu() would have to handle or throw the exception.
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic