• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

doubt in try/catch

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The Output of the following program is:
No such file found
Doing finally
-1
but what I expected was
No such file found
-1
Doing finally
Can any one tell me why?


Thanks in advance

(NOTE: I reformatted your code, you put the whole program on one line, which makes it very hard to read and messes up the forum. Please format your code properly next time - Jesper Young, bartender)
[ July 16, 2007: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

If you are putting return statement in your try/atch block, in that case ,finally will execute before the return statement.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well! This is what i have to say.
If you have a finally block it will always exceute before method terminates, Having return statement doest not matter. Try to understand the flow.
1)You invoke m.amethod().
2)You get a FileNotFoundException.
3)You display -------> "No such file found"
4) Since you have a finally block it will exceute before the method terminates [even if there was no exception, finally will always exceute]
5) Now in the FileNotFoundException catch block you return a value -1.
Before returning from the method, finally block exceutes
In finally block you display -----> ""No such file found""
5) And finally in main the value -1 is displayed
Hence the result is correct.
Hope this clears any confusions.
Thanks
Deepak
 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Now I got the flow .Thanks
 
I found a beautiful pie. And a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic