• 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

exception problem

 
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 !
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic