• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use of finally

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi all,
This is a silly question I am putting here ,I got confused with the basic try catch logic;
please check the code I wrote above.

it gives output 1234
and if I remove finally block from above code ,output is 124.

As far as I remember finally is used to execute important code(cleanup ,message to user etc..) even if there is an exception.
but above code print 4 in both the cases (with /without finally).then whats the use of finally .

guys I think I am missing very basic concept of exception handling. please help.


Thanks



 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Shashank, well you placed a println statement in the finally block and it executes the statement and displays 3 in the output as expected.

In java when you handle an exception, the program executes to completion. Why?...well simply because the catch block took care of the problem and it was NOT propagated to the main method to provoke a JVM shut down. HTH.

Regards

Ikpefua
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The finally-block is guaranteed to execute no matter whether an exception was thrown in the try-block or in the catch-block. Try this code and it will show you why it makes a difference:

And then try this code:
 
reply
    Bookmark Topic Watch Topic
  • New Topic