commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alex Karasulu" <aok...@bellsouth.net>
Subject RE: [digester] pop(), peek() methods don't need to catch exceptions
Date Mon, 05 Apr 2004 02:13:55 GMT


> -----Original Message-----
> From: Simon Kitching [mailto:simon@ecnetwork.co.nz]


> > I was just looking at the digester code as I was writing another
> incarnation
> > of the digester pattern and noticed the pop() and peek() methods do not
> need
> > to catch exceptions.  It is just cheaper to check the size of the stack
> > before the pop() or peek() calls and return null instead of just making
> that
> > call surrounded by a catch block.
> >
> > You guys want me to just make and commit these changes?  It's not really
> a
> > big deal but thought I'd let you guys know about it.
> 
> I don't mind. I don't see any harm, but don't see much benefit either.
> 
> What is the performance of a try/catch clause when exceptions aren't
> thrown, vs an "if (stack.isEmpty())" which is *always* executed? Is it
> truly "cheaper" to use the if?

You really don't know how many times the empty stack is going to have pop or
peek called by a client.  So this really is not a situation that we know is
only going happen once and a while.  "Cheaper" comes from the fact that it
costs let to check if the stack is empty than it does to create an exception
object, fill its stack trace, and hurl it out the door to have it caught
again.

However it certainly is a good point that the conditional test for empty
will always occur.  Making the change really is not a big deal I thought I'd
just point it out.  We can just leave it as is.

> Of course the current exception-based approach is thread-safe, while an
> if-based approach is not, though that is not an issue here.

Please excuse me for being dense but how is throwing the exception a thread
safe situation?  Before the exception is thrown does the stack not check the
same variable to see if it is empty or not? 

	--Alex




---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message