aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception question" Watch "Exception question" New topic
Author

Exception question

Jon Rosenbaum
Greenhorn

Joined: Jul 31, 2001
Posts: 4
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

Joined: Jul 23, 2001
Posts: 49
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

Joined: Jul 31, 2001
Posts: 4
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception question