• 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

How does compiler know that a piece of code requires a checked exception

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does the compiler know that a certain piece of code requires a checked exception. For example if I write the following piece of code
FilInputStream fs=new FileInputStream(File new);
Will it be mandatory for me to catch some exception?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The FileInputStream constructor taking a File object as argument is declared to throw FileNotFoundException (checked) and SecurityException (unchecked) (see API). Since FileNotFoundException extends IOException which in turn extends Exception, it is said to be a checked exception and you have to catch it somehow or throw it further. The compiler knows that all subclasses of Exception that are not subclasses of RuntimeException are checked exception and it can therefore enforce some of its rules. You are not required to catch SecurityException since it is unchecked.
[ January 23, 2003: Message edited by: Valentin Crettaz ]
 
I can't renounce my name. It's on all my stationery! And hinted in 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