Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > is catch and re-throw a good idea?

Reply
Thread Tools

is catch and re-throw a good idea?

 
 
Andy Fish
Guest
Posts: n/a
 
      04-16-2004
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


 
Reply With Quote
 
 
 
 
Niels Dybdahl
Guest
Posts: n/a
 
      04-16-2004
> 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
> }


I would write it like this:

try {
action1;
}
catch (Exception ex) {
undoAction1;
throw ex;
}
action2;

Then there is no need to keep track of whether action1 has completed or not.
You could make a separate handling of action2.

Niels Dybdahl


 
Reply With Quote
 
 
 
 
Andy Fish
Guest
Posts: n/a
 
      04-16-2004

"Niels Dybdahl" <(E-Mail Removed)-graphics.com> wrote in message
news:407f938b$0$160$(E-Mail Removed). ..

> I would write it like this:
>
> try {
> action1;
> }
> catch (Exception ex) {
> undoAction1;
> throw ex;
> }
> action2;
>
> Then there is no need to keep track of whether action1 has completed or

not.
> You could make a separate handling of action2.
>


maybe I wasn't clear enough. I have assumed that both action 1 and 2 are
atomic. If action 2 throws an exception I want to undo action 1. In general
there may be many actions

In my case, I accept the need to keep track of which actions have completed
(and therefore need to be undone in the event of an error); I was more
concerned with whether it is better to re-throw an exception or to do the
undoing in a finally block


 
Reply With Quote
 
Niels Dybdahl
Guest
Posts: n/a
 
      04-16-2004
> maybe I wasn't clear enough. I have assumed that both action 1 and 2 are
> atomic. If action 2 throws an exception I want to undo action 1. In

general
> there may be many actions


In that case your solution is ok.

> In my case, I accept the need to keep track of which actions have

completed
> (and therefore need to be undone in the event of an error); I was more
> concerned with whether it is better to re-throw an exception or to do the
> undoing in a finally block


It is more clear, if you do it in a catch block than in a finally block as
the cleanup is only done on errors.
I have not done any performance tests on the two solutions, but throws
usually happen seldom, so handling them should not be a performance issue.

Niels Dybdahl


 
Reply With Quote
 
antroy
Guest
Posts: n/a
 
      04-16-2004
Andy Fish wrote:
> 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
> }


In my opinion this is the better way. 'finally' is designed as a way to
get a bit of code to run regardless of whether an exception was thrown
or not.

Your code only needs to be run if an exception occurs, so catch is the
right place to do it. The fact that you need to rethrow the exception
doesn't alter this.

--
Ant...
 
Reply With Quote
 
Bent C Dalager
Guest
Posts: n/a
 
      04-16-2004
In article <407fc97a$0$156$(E-Mail Removed)>,
Niels Dybdahl <(E-Mail Removed)-graphics.com> wrote:
>
>It is more clear, if you do it in a catch block than in a finally block as
>the cleanup is only done on errors.
>I have not done any performance tests on the two solutions, but throws
>usually happen seldom, so handling them should not be a performance issue.


As I understand it, neither "throw" nor "catch" cost much. What does
cost is "new Exception()" since that has to create a stacktrace.

Cheers
Bent D
--
Bent Dalager - http://www.velocityreviews.com/forums/(E-Mail Removed) - http://www.pvv.org/~bcd
powered by emacs
 
Reply With Quote
 
Andy Fish
Guest
Posts: n/a
 
      04-16-2004

"antroy" <(E-Mail Removed)> wrote in message
news:c5oiur$m2m$(E-Mail Removed)...
> Andy Fish wrote:
> > 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
> > }

>
> In my opinion this is the better way. 'finally' is designed as a way to
> get a bit of code to run regardless of whether an exception was thrown
> or not.
>
> Your code only needs to be run if an exception occurs, so catch is the
> right place to do it. The fact that you need to rethrow the exception
> doesn't alter this.
>
> --
> Ant...


hmmm, I tried this method but I got stuck and currently have it implemented
using 'finally'.

the problem with the 'catch' method is that I have to catch every possible
type of exception, even unchecked ones, so I have to catch Throwable.
Obviously I don't want to declare my method as 'throws Exception' so in
order to re-throw it I have to say:

if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
} else if (ex instanceof Error) {
throw (Error) ex;
} else if (ex instanceof myExceptionClass) {
throw (myExceptionClass) ex;
}

which is quite messy.

It almost seems like we need a new language construct, something that is a
kind of event notification that an exception occured. 'Catch' carries with
it the inference that I'm going to actually handle the exception (which I'm
not)

Andy



 
Reply With Quote
 
Timo Kinnunen
Guest
Posts: n/a
 
      04-16-2004
On Fri, 16 Apr 2004 12:37:23 +0000 (UTC), Bent C Dalager wrote:

> As I understand it, neither "throw" nor "catch" cost much. What does
> cost is "new Exception()" since that has to create a stacktrace.


The stacktrace is created when throwing, because you can create a new
exception, store it and throw it later.
 
Reply With Quote
 
Suresh
Guest
Posts: n/a
 
      04-16-2004

"Andy Fish" <(E-Mail Removed)> wrote in message
news:z8Rfc.634$(E-Mail Removed)...
>
> "antroy" <(E-Mail Removed)> wrote in message
> news:c5oiur$m2m$(E-Mail Removed)...
> > Andy Fish wrote:
> > > 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
> > > }

> >
> > In my opinion this is the better way. 'finally' is designed as a way to
> > get a bit of code to run regardless of whether an exception was thrown
> > or not.
> >
> > Your code only needs to be run if an exception occurs, so catch is the
> > right place to do it. The fact that you need to rethrow the exception
> > doesn't alter this.
> >
> > --
> > Ant...

>
> hmmm, I tried this method but I got stuck and currently have it

implemented
> using 'finally'.
>
> the problem with the 'catch' method is that I have to catch every possible
> type of exception, even unchecked ones, so I have to catch Throwable.
> Obviously I don't want to declare my method as 'throws Exception' so in
> order to re-throw it I have to say:
>
> if (ex instanceof RuntimeException) {
> throw (RuntimeException) ex;
> } else if (ex instanceof Error) {
> throw (Error) ex;
> } else if (ex instanceof myExceptionClass) {
> throw (myExceptionClass) ex;
> }
>
> which is quite messy.
>
> It almost seems like we need a new language construct, something that is a
> kind of event notification that an exception occured. 'Catch' carries with
> it the inference that I'm going to actually handle the exception (which

I'm
> not)
>
> Andy
>
>
>



Thats not the way to handle the exceptions ...

What book are you reading ?

try{
// Statements ...

}catch (RuntimeException ex) {
// Write logs
// Clean up
throw ex;
}catch(Error ex) {
// Write logs
// Clean up
throw ex;
}catch(myExceptionClass ex) {
// Write logs
// Clean up
throw ex;
}




 
Reply With Quote
 
antroy
Guest
Posts: n/a
 
      04-16-2004
Andy Fish wrote:
....
> the problem with the 'catch' method is that I have to catch every possible
> type of exception, even unchecked ones, so I have to catch Throwable.
> Obviously I don't want to declare my method as 'throws Exception' so in
> order to re-throw it I have to say:



> if (ex instanceof RuntimeException) {
> throw (RuntimeException) ex;
> } else if (ex instanceof Error) {
> throw (Error) ex;
> } else if (ex instanceof myExceptionClass) {
> throw (myExceptionClass) ex;
> }


Have you checked to see that this is necessary? Assuming your action1
and action2 throw a MyException, all you need to declare is that the
method throws a MyException, and catch and re-throw the Throwable and
polymorphism should take care of the rest; declaring that you are
catching a Throwable doesn't stop the exception from being a MyException
nor make RuntimeExceptions or Errors checked:

void method1() throws MyException {

try {
action 1;
action 2;
} catch (Throwable ex) {
if (action 1 completed) {
undo action 1
}
throw ex
}
}


--
Ant...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
catch doesn't catch a thrown exception Marteno Rodia Java 5 08-05-2009 03:30 AM
using try/catch as control mechansim, is it a good thing ? mast2as@yahoo.com C++ 6 03-05-2007 02:57 PM
catch(...) doesn't catch everything Adam C++ 9 02-02-2006 05:02 PM
why catch (...) can not catch such exception John Black C++ 8 08-20-2004 02:34 PM
Trace: Can anyone suggest a good tool to catch trace messages? Rukmal Fernando ASP .Net 4 10-27-2003 09:03 PM



Advertisments