my dog learned polymorphism*
The moose likes Beginning Java and the fly likes finally { } clause in Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "finally { } clause in Exceptions" Watch "finally { } clause in Exceptions" New topic
Author

finally { } clause in Exceptions

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9050
    
  10
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 ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9050
    
  10
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: finally { } clause in Exceptions