File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exception handling" Watch "exception handling" New topic
Author

exception handling

Syamsul Hussin
Ranch Hand

Joined: Feb 09, 2003
Posts: 59
hi, what happens if in the finally block of a try-catch there is a line that uses something in the try block, eg:-

thanks.


----------------------------------<br />SCJP 1.4
Yogini Shirodkar
Greenhorn

Joined: May 22, 2003
Posts: 3
hi
First of all you have to define
BufferedReader input; out side the try block otherwise compiler will throw error when tried to access it frm finally block
After that in the finally bloack
u must catch IOException like this
finally {
try{
input.readLine();
}
catch( java.io.IOException ie){}
}
or otherwise the method in which you have this try block must throw the java.io.IOException

Originally posted by Syamsul Hussin:
hi, what happens if in the finally block of a try-catch there is a line that uses something in the try block, eg:-

thanks.
Gary McGath
Ranch Hand

Joined: Mar 15, 2003
Posts: 52
One other detail may be obvious to some, but this is the beginner forum, so I'll mention it.
When you move the declaration of BufferedReader outside the try block, you have to initialize it, e.g.,
BufferedReader input = null;
Otherwise the compiler will complain that the variable input in the finally block may not have been initialized.


http://www.mcgath.com/consulting/
 
GeeCON Prague 2014
 
subject: exception handling