Cost of having try-catch statements?

Andrew Haley aph@redhat.com
Tue Aug 10 19:36:00 GMT 2004


Harald Niesche writes:
 > Andrew Haley wrote:
 > 
 > > Martin Egholm Nielsen writes:
 > >  > >  > I was wondering: Is there any significant cost in having "unused" 
 > >  > >  > try-catches in my code?
 > >  > >  > That is, I have a method that 99.99% of the times invoked never throws a 
 > >  > >  > given exception. However, in case it does, I need to log it...
 > >  > > The only cost is the unwinder tables, (which are not read into memory
 > >  > > unless your exception is thrown) and the handler itself, which may
 > >  > > cause some instruction cache presure.
 > >  > So practically no overhead?
 > > 
 > > That's the idea.  Really, if there is any significant overhead for an
 > > unused handler that would be a bug.
 > 
 > Being curious, I tried a micro benchmark (Main.java, attached).
 > 
 > The code just calls a method on an instance of class Main that then 
 > proceeds to call another method (that will never throw an exception, it 
 > simply increments a member to be not completely empty). In the first 
 > case there is nothing happening besides the second function being 
 > called, in the second case I have wrapped a try/catch around the call.
 > 
 > 
 > Harald
 > PS: I ran this on a 1GHz Centrino Notebook with 512mb Ram, CPU speed 
 > locked at 1GHz

Ah yes, Windows still uses longjmp.

Similar hardware, Linux:

without try/catch 207ms
with try/catch 227ms
without try/catch 202ms
with try/catch 209ms

Andrew.



More information about the Java mailing list