Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception question

 
Jon Rosenbaum
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
Alex Sbityakov
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic