Re: Exceptions no longer incur overhead?
- From: dagon@xxxxxxxxx (Mark Rafn)
- Date: Fri, 29 Jun 2007 16:03:55 -0700
void.no.spam.com@xxxxxxxxx <void.no.spam.com@xxxxxxxxx> wrote:
One of our classes has a method to look up an object. If the object
does not exist, it throws an exception to indicate that. I thought it
would be better to just return null, as exceptions incur overhead.
Like most micro-optimization, this should take a backseat to clarity and ease
of understanding. What are the semantics that best fit your use cases?
Exceptions are nice because they indicate exceptional cases, and you can
declare them to make them explicit, and they're easy to bubble up to the
appropriate handling level.
Null returns are nice because they fit the developer's mental model of
many types of find operations, but you have to document and hope callers
will understand what it means.
Performance is a secondary (or later) consideration.
I talked to the senior java developer who wrote the class, and he said
that recent versions of Java have improved exceptions to the point
that they do not really incur any overhead. Is this true?
Exception declarations incur very little overhead. Just having a
try/catch structure in your code used to be somewhat slower than code
without it, but that's not true in modern VMs. Actually throwing an
exception is a little slower than other return types.
But nothing costs as much as an API that doesn't fit the usage patterns.
--
Mark Rafn dagon@xxxxxxxxx <http://www.dagon.net/>
.
- References:
- Exceptions no longer incur overhead?
- From: void.no.spam.com@xxxxxxxxx
- Exceptions no longer incur overhead?
- Prev by Date: Re: Exceptions no longer incur overhead?
- Next by Date: Re: New Java Site
- Previous by thread: Re: Exceptions no longer incur overhead?
- Next by thread: Re: Exceptions no longer incur overhead?
- Index(es):
Relevant Pages
|