• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

exception problem

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

public void myMethod throws Exception {
try {
// line 1
...
} finally {
// line2
// line 3
}
}

if Exception coours at "line 2" then "line 3" will NOT be executed.

try this

finally {
try {
// line 2;
} catch (Exception e) { throw e; }
// line 3
}

if Exception occurs at "line 2", "line 3" still not executed.

try this

finally {
try {
// line 2;
} catch (Exception e) {
System.out.println("don't handle it");
}
// line 3;
}

in this case, when Exception occurs in "line 2", "line 3" is executed.


Can you explain why "line 3" is not executed in the second snippet but in the 3rd snippet ?
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sure...

in the second snippet a new exception is thrown, so the programm flow is intercepted _again_.

in the third snippet the exception handling terminates completely, so the control flow continues with the next regular line.

hope it helps,
jan
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
btw - i didn't see the embrassing "finaly".

i'm sure you know that if you want the syntax to be legal, the outer finaly needs an outer try block, and is technically unrelated to the inner logic of your examples...
 
Artemesia Lakener
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks !
 
I don't always make ads but when I do they're tiny
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic