If you want to read a file that is made up of lines of text, then that is a good way to do it. But it could be improved a little bit. For example, if there is an IOException then you don't do anything and you don't tell anybody it happened. That makes it hard to identify problems when they occur. Also, if an exception occurs then it's possible you don't close the file properly. That can leave the file locked and prevent other tasks from accessing it. Handling the exceptions properly looks more like this:The "finally" block in that code will be called even if an exception occurred inside the "try" block. Even if it was a run-time exception like NullPointerException which you didn't catch.
posted 14 years ago
Thank you Paul,
You was right, i missed lots of things there and thanks for a nice code example. I have however, one question about finally clause. If I use finally, do i still need a catch clause before finally?