• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

exception problem  RSS feed

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
thanks !
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!