Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exception handling

 
Syamsul Hussin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Yogini Shirodkar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic