Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java Exceptions cause performance hit?

Reply
Thread Tools

Java Exceptions cause performance hit?

 
 
kk_oop@yahoo.com
Guest
Posts: n/a
 
      07-13-2005
Hi. I wanted to use exceptions to handle error conditions in my code.
I think doing that is useful, as it helps to separate "go" paths from
error paths. However, a coding guideline has been presented that says
"Use conventional error-handling techniques rather than exception
handling for straightforward local error processing in which a program
is easily able to deal with its own errors."

By "conventional error-handling," I believe they mean returning an
error code, or just handling the error without going to a catch block.


When I said that I'd prefer throwing and catching exceptions--and that,
in fact, exceptions is the "conventional error-handling technique" in
Java, I was told that since we have a real-time system, we can't afford
the performance hit caused by using exceptions.

Do exception blocks cause big performance hits? If so, what causes the
hit? Or is the person just misinformed?

Thanks for any info,

Ken

 
Reply With Quote
 
 
 
 
Steve Wampler
Guest
Posts: n/a
 
      07-13-2005
On Wed, 13 Jul 2005 08:21:12 -0700, kk_oop wrote:

> Do exception blocks cause big performance hits? If so, what causes the
> hit? Or is the person just misinformed?


I don't know how Java implements exception handling, but having
done it in another language I would say that the only significant
performance hit *should* be the construction of the stack trace
(and even there one could argue about the definition of
'significant'...). Plus, the ability of an thrown exception to
perform a 'deep return' may help mitigate that cost.

Personally, I think the benefit outweighs the cost, but I could
understand someone (especially in a RT system) possibly wanting
to cut that cost out - particularly if the stack trace could be
turned on/off dynamically).

One wonders, however, what Java version you're using in a
real-time system...


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-13-2005


wrote:
> Hi. I wanted to use exceptions to handle error conditions in my code.
> I think doing that is useful, as it helps to separate "go" paths from
> error paths. However, a coding guideline has been presented that says
> "Use conventional error-handling techniques rather than exception
> handling for straightforward local error processing in which a program
> is easily able to deal with its own errors."
>
> By "conventional error-handling," I believe they mean returning an
> error code, or just handling the error without going to a catch block.
>
>
> When I said that I'd prefer throwing and catching exceptions--and that,
> in fact, exceptions is the "conventional error-handling technique" in
> Java, I was told that since we have a real-time system, we can't afford
> the performance hit caused by using exceptions.
>
> Do exception blocks cause big performance hits? If so, what causes the
> hit? Or is the person just misinformed?


Creating, throwing, catching, and eventually garbage-
collecting a Throwable object is certainly a lot more work
than a simple `if'. You can measure it for yourself by
comparing the time required for two different ways of
filling an array:

for (int i = 0; i < array.length; ++i)
array[i] = i;

try {
for (int i = 0; ; ++i)
array[i] = i;
} catch (ArrayIndexOutOfBoundsException ex) {}

(This is pretty much the same experiment Joshua Bloch shows
in "Effective Java.") On the machine I'm using at the moment,
the exception-based approach takes 15.5 times as long as the
straightforward loop to fill a 100-element array; that is, one
exception takes longer than 1500 limit tests. Your machine
may give different timings; try it and see.

Note that the guidance you've been given does not forbid
the use of exceptions; it only forbids them for "straightforward
local error processing." I suggest you go back to the folks
who wrote the guidelines and ask what "straightforward" means;
it will probably turn out to be less burdensome than you seem
to fear.

--


 
Reply With Quote
 
Paul Bilnoski
Guest
Posts: n/a
 
      07-13-2005
wrote:

> When I said that I'd prefer throwing and catching exceptions--and that,
> in fact, exceptions is the "conventional error-handling technique" in
> Java, I was told that since we have a real-time system, we can't afford
> the performance hit caused by using exceptions.
>
> Do exception blocks cause big performance hits? If so, what causes the
> hit? Or is the person just misinformed?
>


The problem with exceptions in hard real-time systems is that it
introduces more invariants with respect to the expense of execution. It
becomes more difficult to make guarantees about time and memory used, so
I think that may be why it is preferred against when you can use
something conventional in simpler cases.
There may be some threshold of complexity where you opt for an exception
over checking flags or error codes.
There is a similar problem with garbage collection and making running
time more predictable that RT implementations account for.
--Paul
 
Reply With Quote
 
Andrea Desole
Guest
Posts: n/a
 
      07-13-2005


Eric Sosman wrote:

> Note that the guidance you've been given does not forbid
> the use of exceptions; it only forbids them for "straightforward
> local error processing." I suggest you go back to the folks
> who wrote the guidelines and ask what "straightforward" means;
> it will probably turn out to be less burdensome than you seem
> to fear.


Good point.
I was just wondering why worry about performance if an exception is for,
well, exceptional cases. If an exception is being thrown so often to
have a performance impact, either you have a problem with your code or
you have a problem with your system.
Maybe that person just wants to prevent exception abuse
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      07-13-2005
Paul Bilnoski wrote:
> wrote:
>
>> When I said that I'd prefer throwing and catching exceptions--and
>> that, in fact, exceptions is the "conventional error-handling
>> technique" in Java, I was told that since we have a real-time
>> system, we can't afford the performance hit caused by using
>> exceptions.
>>
>> Do exception blocks cause big performance hits? If so, what causes
>> the hit? Or is the person just misinformed?


A thrown exception causes the hit. IMHO the penalty in the normal case
(no exception thrown) is neglectible.

> The problem with exceptions in hard real-time systems is that it
> introduces more invariants with respect to the expense of execution.
> It becomes more difficult to make guarantees about time and memory
> used, so I think that may be why it is preferred against when you can
> use something conventional in simpler cases.
> There may be some threshold of complexity where you opt for an
> exception over checking flags or error codes.
> There is a similar problem with garbage collection and making running
> time more predictable that RT implementations account for.


As far as I understand the term "real time system" I wonder why someone
uses Java in the first place. There is no such thing as guaranteed
response times etc. So maybe the OP is dealing with a soft real time
system.

http://en.wikipedia.org/wiki/Real-time_system

Regards

robert

 
Reply With Quote
 
Paul Bilnoski
Guest
Posts: n/a
 
      07-13-2005
Robert Klemme wrote:
> Paul Bilnoski wrote:
>
>> wrote:
>>
>>
>>>When I said that I'd prefer throwing and catching exceptions--and
>>>that, in fact, exceptions is the "conventional error-handling
>>>technique" in Java, I was told that since we have a real-time
>>>system, we can't afford the performance hit caused by using
>>>exceptions.
>>>
>>>Do exception blocks cause big performance hits? If so, what causes
>>>the hit? Or is the person just misinformed?

>
>
> A thrown exception causes the hit. IMHO the penalty in the normal case
> (no exception thrown) is neglectible.
>
>
>>The problem with exceptions in hard real-time systems is that it
>>introduces more invariants with respect to the expense of execution.
>>It becomes more difficult to make guarantees about time and memory
>>used, so I think that may be why it is preferred against when you can
>>use something conventional in simpler cases.
>>There may be some threshold of complexity where you opt for an
>>exception over checking flags or error codes.
>>There is a similar problem with garbage collection and making running
>>time more predictable that RT implementations account for.

>
>
> As far as I understand the term "real time system" I wonder why someone
> uses Java in the first place. There is no such thing as guaranteed
> response times etc. So maybe the OP is dealing with a soft real time
> system.
>
> http://en.wikipedia.org/wiki/Real-time_system
>
> Regards
>
> robert
>


I used Java for a few real-time system programming projects in college.
We had a RT Linux kernel and the distributor also provided an RT Java VM
implementation though it was not the full language (no 1.4 NIO, no Swing).
There was also some extra functionality for the GC system to make it
handle in a more predictable way.

But to answer your point of "why use Java": because it is more
comfortable to program multithreaded applications than with some C++
threading libraries, among other things.
--Paul
 
Reply With Quote
 
Casey Hawthorne
Guest
Posts: n/a
 
      07-13-2005
Exceptions have performance penalties when thrown, only minor when not
thrown!
Depending upon how you define "minor"!

The challenge with exceptions is maintaining global state, like:
- cursor state
- timing state in real-time systems

--
Regards,
Casey
 
Reply With Quote
 
Cantankerous Old Git
Guest
Posts: n/a
 
      07-13-2005
Andrea Desole wrote:
>
>
> Eric Sosman wrote:
>
>> Note that the guidance you've been given does not forbid
>> the use of exceptions; it only forbids them for "straightforward
>> local error processing." I suggest you go back to the folks
>> who wrote the guidelines and ask what "straightforward" means;
>> it will probably turn out to be less burdensome than you seem
>> to fear.

>
>
> Good point.
> I was just wondering why worry about performance if an exception is for,
> well, exceptional cases. If an exception is being thrown so often to
> have a performance impact, either you have a problem with your code or
> you have a problem with your system.
> Maybe that person just wants to prevent exception abuse


I haven't done any testing, so I speak in ignorance, but could it
be that setting up the try/catch block is expensive? This will
have to happen whether an Exception is actually thrown or not,
and will therefore affect error-free performance too.

The Cog
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-13-2005


Cantankerous Old Git wrote:
> Andrea Desole wrote:
>
>>
>>Eric Sosman wrote:
>>
>>
>>> Note that the guidance you've been given does not forbid
>>>the use of exceptions; it only forbids them for "straightforward
>>>local error processing." I suggest you go back to the folks
>>>who wrote the guidelines and ask what "straightforward" means;
>>>it will probably turn out to be less burdensome than you seem
>>>to fear.

>>
>>
>>Good point.
>>I was just wondering why worry about performance if an exception is for,
>>well, exceptional cases. If an exception is being thrown so often to
>>have a performance impact, either you have a problem with your code or
>>you have a problem with your system.
>>Maybe that person just wants to prevent exception abuse

>
>
> I haven't done any testing, so I speak in ignorance, but could it
> be that setting up the try/catch block is expensive? This will
> have to happen whether an Exception is actually thrown or not,
> and will therefore affect error-free performance too.


Look at the bytecode, and you'll see that it's very
cheap. The cost amounts to one unconditional jump at the
end of the `try' clause to skip over the `catch' clause(s).
It's possible that the JIT compiler might reorganize the
compiled code to eliminate even that much overhead.

--



 
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
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
C++ Exceptions Cause Performance Hit? kk_oop@yahoo.com C++ 59 07-28-2005 11:57 AM
will all these messages cause a problem . I am a new subscriber and my computer is downloading 100,000 messages. Will this cause any kind of a problem with my ability to store other items?? Camille White Camille White Computer Support 9 11-08-2004 01:13 AM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57