• 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

Exception question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Initially, I'll just introduce myself to the board. My name's Jon and I'm currently aspiring to become a certified Java programmer.
I have a question on the following code from Marcus Green's tutorial site:


My question is this: since there is a try/catch clause to handle an IOException, how come the main method can be declared without a "throws IOException"?
Thanks.
Jon
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jon,
There are two ways of dealing with exceptions. One way is with a try catch block as is shown in the code. This way, we deal with the exception locally. The second method is with a throws clause. What this means, is we are not going to deal with the exception locally, but propagate it to the caller, so the caller will have to place the method in a try catch block.
However, if your try catch block throws an exception then you'll have to use the throws clause to show that the method may itself throw an exception.
 
Jon Rosenbaum
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alex,
Thank you very much for your response. I now better understand when try/catch and throws get used.
Jon

Originally posted by Alex Sbityakov:
Jon,
There are two ways of dealing with exceptions. One way is with a try catch block as is shown in the code. This way, we deal with the exception locally. The second method is with a throws clause. What this means, is we are not going to deal with the exception locally, but propagate it to the caller, so the caller will have to place the method in a try catch block.
However, if your try catch block throws an exception then you'll have to use the throws clause to show that the method may itself throw an exception.


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic