*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exception handling
 
Similar Threads
Exception in finally
IBM Mock Exam: Try Catch Finally ?
try catch finally blocks
Need for finally block?
Can we use finaly block to catch exceptions without using catch block?