This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes try without catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "try without catch" Watch "try without catch" New topic
Author

try without catch

Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104


I am getting the below error while compiling the code, please explain:

MyCatch.java:9: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
FileInputStream f1 = new FileInputStream(f);
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Originally posted by Manish Singhal:


I am getting the below error while compiling the code, please explain:

MyCatch.java:9: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
FileInputStream f1 = new FileInputStream(f);



The line of code
FileInputStream f1 = new FileInputStream(f);
When you read in the API you will see that this throws an exception. If it throws an exception, the line of code must be placed in a try catch block that catches all of the exceptions.

You can also catch the biggest exception and deal with that one.

Example, for your code you could use either of the following three pieces of code...

try{
FileInputStream f1 = new FileInputStream(f);
}catch(FileNotFoundException fnfe){}
catch(IOException ioe){}


or

try{
FileInputStream f1 = new FileInputStream(f);
}catch(IOException ioe){}


or

try{
FileInputStream f1 = new FileInputStream(f);
}catch(Exception e){}

The first one is preferable

Cheers,
Rachel
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
Rachel, I agree with what you say but here would like to mention:

Question: Is the following code legal?
try {
...
} finally {
...
}
Answer: Yes, it's legal. A try statement does not have to have a catch statement if it has a finally statement. If the code in the try statement has multiple exit points and no associated catch clauses, the code in the finally statement is executed no matter how the try block is exited.

Any comments !!!
[ May 26, 2004: Message edited by: Manish Singhal ]
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Surely if you have code in the try bloc that is going to cause an exception to be thrown that you need a catch block to catch the exception? Maybe not to compile it but you need it from a logic point.

Even when you compiled it, it said that you were not catching the exception. And if you write code that does not throw an exception then why do you want to do it in a try finally block?
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
A try - finally block is used usually in a method that declares to throw any Checked Exceptions that can occur. in your example, if you changed the declaration of main to be:
public static void main(String args[]) throws IOException
The code will compile just fine.

Typically, you use try - finally when you want to ensure some cleanup is done, even if an Exception is thrown but, do not wish to handle the Exception thrown in any way.A fair example would be SQL code


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Well, the way that I code at the moment is to deal with java's exceptions when they occur. Could you give me an example of when to keep throwing the exceptions would be better?
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104
Carl, thanks for the clear explanation.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Rachel, sometimes the information necessary to deal with the exception is not present in the object containing the method in which the exception was thrown. The exception can progress untill is caught by a method in an object that knows how to recover. Doing so you can centralize the exception handling code for a family of exceptions.


SCJP2. Please Indent your code using UBB Code
 
GeeCON Prague 2014
 
subject: try without catch