Reputation: 520
i am a java newbie. i have a question regarding how to organize java code when using try catch finally blocks. suppose i have to read some text files and do some computations on the stored file contents. how should my code look like?
For example
code 1 looks like:
public static void main(String[] args){
try{
//open files using BufferedReader, read and store the file contents.
}catch(IOException e){
e.printStackTrace();
}
finally{
//close the files
}
// do computations on the data
}
code 2 looks like:
public static void main(String[] args){
try{
//open files using BufferedReader, read and store the file contents.
// do computations on the data
}catch(IOException e){
e.printStackTrace();
}
finally{
//close the files
}
}
which of the two is a better coding practice? Also should finally block be placed just after try catch or it can be placed towards the end.
Upvotes: 5
Views: 1360
Reputation: 4223
This depends on your system specifics, I see two possible cases:
1)File is large. Then you cant load it into the memmory. And you should read pices of file one by one and do your logic on every pice. This will prevent you from running out of memmory.
2)File is small. Then you have a choice, wheather to follow the first case or read file in memmory, close it and then process it.
There is one more consideration: while file is not closed, other processes cannot access it. That could be a problem if your logic is very long, in such case reading and closing a file first is better.
Upvotes: 0
Reputation: 21993
The second.
The idea with exceptions is you throw them at the point where a problem is detected and they are catched not earlier then the first point where the earlier problem does no longer affects the execution of the code. Possibly by doing some handling in the catch block and some cleanup using a finally block.
In your first code sample the code following the catch is still affected by the error that caused the exception. So you caught it to early. Which means you may have to add an if statement around it which unnecessarily raises the complexity of the function.
Upvotes: 0
Reputation: 98
The second method should be used because it is going to be easier to make computations on the data without running into variable scope issues. However, both ways can work depending on the computations that need to be done.
Upvotes: 0
Reputation: 60768
Use Java 7 and try-with-resources.
try(Connection = pool.getConnection()) { // or any resource you open, like files
// ...
} // auto closes
This feature comes close to deprecating finally
- I have personally not found a use case for finally
once this feature was added and suggest you avoid it. It's like goto
or arguably continue
, or even for
loops as far as functional programming is concerned - newer features have made the use unnecessary.
Upvotes: 2
Reputation: 12123
Finally block should be placed just after try-catch. Where you want to do the computations on your data is up to you... but if you put it after the try-catch block, it will attempt the computations even if an exception is thrown, unless you use a condition.
Upvotes: 1