Re: Exceptions no longer incur overhead?



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/>
.



Relevant Pages

  • Two questions about efficiency
    ... Near the beginning of the document "Unifying types and classes in ... Python 2.2" by GvR, ... The exception would be when we expect that the requested ... have thought that the overhead to do a key lookup is quite a bit less ...
    (comp.lang.python)
  • Re: Method Design Architecture guidance needed...
    ... The performance hit I was referring to was for built-in exception ... To make an exception processor bullet-proof (e.g., ... overhead beyond a simple context switch. ...
    (comp.object)
  • Re: RTTI overhead
    ... >>overheads (unless an exception is thrown in which case what should be ... >>considered as overhead is not clear). ... directly or otherwise, whatever else happens - stack unwinding, ... <end musing> ...
    (comp.lang.cpp)
  • Re: advice on best use of try catch throw
    ... which presents an additional overhead. ... It encourages less good programmers to ignore possible errors. ... Recurring checks of returnvalues are an unnecessary performance overhead. ... Exception can carry much more information than a HRESULT. ...
    (microsoft.public.vc.language)
  • Re: Where to start the try block and whats the overhead???
    ... try blocks add overhead when they are fired or spring into action. ... The issue with wrapping a big nasty try block is that you aren't ... really able to tell what entity fired the exception. ... blow the application apart - a stack overflow or memory exhaustion exception ...
    (microsoft.public.dotnet.languages.csharp)