aspose file tools*
The moose likes Beginning Java and the fly likes try...catch...finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "try...catch...finally" Watch "try...catch...finally" New topic
Author

try...catch...finally

Subhajit Mitra
Greenhorn

Joined: Jun 06, 2006
Posts: 21
let's consider two code

code 1:

try {
errorProne();

}
catch(Exception allError) {
toHandle(allError);
}
finally {
mustExecute();
}

code 2:

try {
errorProne();

}
catch(Exception allError) {
toHandle(allError);
}
mustExecute();

are they same? will there be any situation when mustExecute() will not execute in code2?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
This sounds like an execise. Why are you asking?
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Yes.

http://java.sun.com/docs/books/tutorial/essential/exceptions/handling.html
Subhajit Mitra
Greenhorn

Joined: Jun 06, 2006
Posts: 21
it's not a normal code copied from textbook nor i need to submit an assignment. its my own code
in this case "Exception" has been caught which is the base class for all exception so under all normal case catch will be able to catch and there is no need to give finally

i was wondering about special cases like
System.exit(0)
or
Process p = Runtime.getRuntime().exec("<o/s kill command>");

as far as i know if i execute these codes in try in both cases mustExecute() will not execute. but is there any special case where code 1 prevails over code 2
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
You're missing the most obvious which is what happens if "toHandle" exits abruptly.
stephen shields
Greenhorn

Joined: Jun 05, 2006
Posts: 20
this does seem like a homework assignment.
in my limited knowledge, yes they are the same. finally just says to execute this code if an exception is after all exceptions are by passed.
if an exception is caught, all work following it will not occur
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by stephen shields:
this does seem like a homework assignment.
in my limited knowledge, yes they are the same. finally just says to execute this code if an exception is after all exceptions are by passed.
if an exception is caught, all work following it will not occur


Once again, no they are not the same. Under normal circumstances code in a finally block will always execute. If the try block completes normally it will be executed. If the try block throws an exception, it will still execute. If the exception is caught by a catch block, it will still execute. If an exception is caught by a catch block and the catch block throws another exception, it will still execute.

Nothing can cause a finally block to be skipped, however an infinite number of possibilities can cause the JVM to terminate before the finally block can be executed. If a meteor hits the machine, or the power goes off, or the processor blows up, or there's a call to System.exit(), or the application is suddenly terminated, etc. the finally block may not be executed, but there's nothing you can do about that.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
If a meteor hits the machine


Man, I hate when that happens.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
stephen shields
Greenhorn

Joined: Jun 05, 2006
Posts: 20
lol damn i feel stupid but its ok
posted it while bored
you are right tho. its just that me i never use a finally bc if im designing a gui application, which is in my experience where alot of exceptions are caught, if an exception is caught, i would prefer that a message gets sent to the user.
any code i want to occur when the exception is caught, such as system exit, etc, i make to execute in the catch. now i no i can put it in the finally. but hey we all have our ways. thanks for correcting my mistake tho
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
Stephen, remember that the finally will run even if an exception was caught by a preceeding catch block. You can still handle the exception, and send off a message to the user inside the catch, and then clean up anything you need to in the finally block (close database connections, etc).
pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
Ken Blair wrote:
Originally posted by stephen shields:
this does seem like a homework assignment.
in my limited knowledge, yes they are the same. finally just says to execute this code if an exception is after all exceptions are by passed.
if an exception is caught, all work following it will not occur


Once again, no they are not the same. Under normal circumstances code in a finally block will always execute. If the try block completes normally it will be executed. If the try block throws an exception, it will still execute. If the exception is caught by a catch block, it will still execute. If an exception is caught by a catch block and the catch block throws another exception, it will still execute.

Nothing can cause a finally block to be skipped, however an infinite number of possibilities can cause the JVM to terminate before the finally block can be executed. If a meteor hits the machine, or the power goes off, or the processor blows up, or there's a call to System.exit(), or the application is suddenly terminated, etc. the finally block may not be executed, but there's nothing you can do about that.



i dont get it.
System.exit() will terminate the JVM but it's true for both the cases.

so 1st & 2nd code is same.

finally will execute in all cases (except JVM crashes) and in first code, mustExecute() will also execute in all cases.



:d
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

First of all, you do know that this is a three year old topic, right? And that maybe many of the participants have moved on?

pooja jain wrote:
i dont get it.
System.exit() will terminate the JVM but it's true for both the cases.


The exit() discussion was a side discussion about finally -- and not really on-topic.

pooja jain wrote:
so 1st & 2nd code is same.


Not true. See next...

pooja jain wrote:
finally will execute in all cases (except JVM crashes) and in first code, mustExecute() will also execute in all cases.


Right, but only the first case has a finally -- so in the second case it is possible for mustExecute() to not execute.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Henry Wong wrote:And that maybe many of the participants have moved on?
We know that one of them, sadly, has died.
pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
i am really sorry to hear that.

actually this thread was referred in one of the recent thread and so i happen to post in it.

@henry
Right, but only the first case has a finally -- so in the second case it is possible for mustExecute() to not execute.


in what circumstances, it's possible for mustExecute() to not execute? that is what my confusion is.

tia.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

pooja jain wrote:@henry
Right, but only the first case has a finally -- so in the second case it is possible for mustExecute() to not execute.

in what circumstances, it's possible for mustExecute() to not execute? that is what my confusion is.


1. errorProne() throws an Error.
2. errorProne() throws an exception, which is followed by toHandle() throws an Error.
3. errorProne() throws an exception, which is followed by toHandle() throws a runtime exception.

Henry
pooja jain
greenhorn
Ranch Hand

Joined: Jan 12, 2005
Posts: 213
Henry Wong wrote:
pooja jain wrote:@henry
Right, but only the first case has a finally -- so in the second case it is possible for mustExecute() to not execute.

in what circumstances, it's possible for mustExecute() to not execute? that is what my confusion is.


1. errorProne() throws an Error.
2. errorProne() throws an exception, which is followed by toHandle() throws an Error.
3. errorProne() throws an exception, which is followed by toHandle() throws a runtime exception.

Henry


yes, you are right.
what if i do this:


try {
errorProne();
} catch(Throwable t) {

}
mustExecute();

now point 1 is handelled.
point 2 & 3 also handelled because i am not doing anything in catch block so no exception or error can be thrown.

hope you get it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try...catch...finally