Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finally { } clause in Exceptions

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And WHY is the finally clause executed if NO condition for an exception
is fullfilled AS i=2 => break!!!

finally clauses are always executed unless you call System.exit in your catch block (which I do not recommend).

Gotta run.
[ August 04, 2002: Message edited by: Marilyn de Queiroz ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas,
Note that the finally block is always executed, even if the try or catch block returns. As Marilyn noted, it's only not called when the try or catch block shuts down the JVM with a call to System.exit( int ) or if a very serious error condition is raised, such as running out of memory, which also causes the JVM to shut down (I think).
Also, to make your posts even easier to read, you may only want to surround actual code blocks with the [code] and [/code] UBB Tags.
Good Luck.
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only have zero or one finally for each try statement, and only one catch statement will execute for each try (which is why you should catch the most specific one first if you have more than one). That's just the way it is.

try{
}
catch ( SpecificException se ){ // i.e. FileNotFoundException
}
catch ( GeneralException ge ){ // i.e. IOException
}
catch ( Exception e ){ // i.e. Exception
}
finally{
}

try executes, then one of the catch statements, then finally.
[ August 04, 2002: Message edited by: Marilyn de Queiroz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic