• 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

Exception logic doubt

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

In the above code I expect runtime exception but the code compiles and run giving output 3. What happens to the runtime error at //1
I can't understand the logic, can any one guide please!!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the above code I expect runtime exception but the code compiles and run giving output 3. What happens to the runtime error at //1



The runtime exception at "//1" got hidden/replaced by the value returned in the finally clause. This is why you should not throw exceptions or return values from the finally clause -- any exceptions thrown or value returned from the try/catch blocks will not complete.

Henry
 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand the above explantion like below
when an runtime exception occured inside a catch block and a finally clause is included then that exception is ignored/hidden. Am i right?

another doubt regarding the same code.
If any exception occured inside a catch block, still finally block run?
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

when a finally-block is declared, this finally-block is always called, whether there is an exception or not AND whether an exception is thrown.

A finally is not called, when the try/catch-part would shut down the virtual machine, using a System.exit(). But where would be the sense here?

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


i got that runtimeException is hidden ,return statements of try and catch are hidden and finally gets executed.but i am confused how do we get the value 3.Please help me with which statements got executed.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both System.out.println()-statements will each decrement x by 1, so the output is 3 (return x-- in finally block will return the actual value of x, which is 3).
 
reply
    Bookmark Topic Watch Topic
  • New Topic