Note that there are some explanatory texts on larger screens.

plurals
  1. POHow slow are Java exceptions?
    primarykey
    data
    text
    <p>Question: Is exception handling in Java actually slow?</p> <p>Conventional wisdom, as well as a lot of Google results, says that exceptional logic shouldn't be used for normal program flow in Java. Two reasons are usually given,</p> <ol> <li>it is really slow - even an order of magnitude slower than regular code (the reasons given vary), </li> </ol> <p>and </p> <ol start="2"> <li>it is messy because people expect only errors to be handled in exceptional code. </li> </ol> <p>This question is about #1.</p> <p>As an example, <a href="http://leepoint.net/notes-java/flow/exceptions/03exceptions.html" rel="noreferrer">this page</a> describes Java exception handling as "very slow" and relates the slowness to the creation of the exception message string - "this string is then used in creating the exception object that is thrown. This is not fast." The article <a href="http://adtmag.com/articles/2000/08/22/effective-exception-handling-in-java.aspx" rel="noreferrer">Effective Exception Handling in Java</a> says that "the reason for this is due to the object creation aspect of exception handling, which thereby makes throwing exceptions inherently slow". Another reason out there is that the stack trace generation is what slows it down.</p> <p>My testing (using Java 1.6.0_07, Java HotSpot 10.0, on 32 bit Linux), indicates that exception handling is no slower than regular code. I tried running a method in a loop that executes some code. At the end of the method, I use a boolean to indicate whether to <em>return</em> or <em>throw</em>. This way the actual processing is the same. I tried running the methods in different orders and averaging my test times, thinking it may have been the JVM warming up. In all my tests, the throw was at least as fast as the return, if not faster (up to 3.1% faster). I am completely open to the possibility that my tests were wrong, but I haven't seen anything out there in the way of the code sample, test comparisons, or results in the last year or two that show exception handling in Java to actually be slow.</p> <p>What leads me down this path was an API I needed to use that threw exceptions as part of normal control logic. I wanted to correct them in their usage, but now I may not be able to. Will I instead have to praise them on their forward thinking?</p> <p>In the paper <a href="http://portal.acm.org/citation.cfm?id=337453" rel="noreferrer">Efficient Java exception handling in just-in-time compilation</a>, the authors suggest that the presence of exception handlers alone, even if no exceptions are thrown, is enough to prevent the JIT compiler from optimizing the code properly, thus slowing it down. I haven't tested this theory yet.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI know you were't asking about 2), but you really should recognize that using an exception for program flow is no better than using GOTOs. Some people defend gotos, some people would defend what you are talking about, but if you ask someone who has implemented and maintained either for a period of time, they will tell you that both are poor hard to maintain design practices (and will probably curse the name of the person who thought they were smart enough to make the decision to use them).
      singulars
    2. COBill, claiming that using exceptions for program flow is no better than using GOTOs is no better than claiming that using conditionals and loops for program flow is no better than using GOTOs. It's a red herring. Explain yourself. Exceptions can and are used effectively for program flow in other languages. Idiomatic Python code uses exceptions regularly, for instance. I can and have maintained code that uses exceptions in this way (not Java though), and I don't think there's anything inherently wrong with it.
      singulars
    3. CONote that some web frameworks use Exceptions as a convenient way to redirect - e.g. Wicket's [RestartResponseException](http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/RestartResponseException.html). It happens just few times per request, usually not, and I can hardly imagine more convenient way in a Java-oriented component framework.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload