Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

exception handling

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic