A
Andy Fish
Hi,
I have a scenario where I need to "undo" some processing when an exception
occurs but I don't want to otherwise interfere with the handling of the
exception. one way seems to be catching and re-throwing it thus:
try {
action 1;
action 2;
} catch (Exception ex) {
if (action 1 completed) {
undo action 1
}
throw ex
}
another way would be using the finally clause. This seems structurally
better because I'm not interfering with the exception itself. However,
because it gets executed even if there hasn't been an exception, I need to
use an extra local variable to figure out whether there was actually an
exception at all. maybe I am abusing the finally block to do exception
handling?
try {
action 1;
action 2;
finished=true;
} finally {
if (!finished) {
if (action 1 completed) {
undo action 1
}
}
}
anyone have any thoughts on which is best? is there a common idiom for this?
Andy
I have a scenario where I need to "undo" some processing when an exception
occurs but I don't want to otherwise interfere with the handling of the
exception. one way seems to be catching and re-throwing it thus:
try {
action 1;
action 2;
} catch (Exception ex) {
if (action 1 completed) {
undo action 1
}
throw ex
}
another way would be using the finally clause. This seems structurally
better because I'm not interfering with the exception itself. However,
because it gets executed even if there hasn't been an exception, I need to
use an extra local variable to figure out whether there was actually an
exception at all. maybe I am abusing the finally block to do exception
handling?
try {
action 1;
action 2;
finished=true;
} finally {
if (!finished) {
if (action 1 completed) {
undo action 1
}
}
}
anyone have any thoughts on which is best? is there a common idiom for this?
Andy