Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 80913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:22:38+00:00 2026-05-10T21:22:38+00:00

Question: Is exception handling in Java actually slow? Conventional wisdom, as well as a

  • 0

Question: Is exception handling in Java actually slow?

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,

  1. it is really slow – even an order of magnitude slower than regular code (the reasons given vary),

and

  1. it is messy because people expect only errors to be handled in exceptional code.

This question is about #1.

As an example, this page 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 Effective Exception Handling in Java 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.

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 return or throw. 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.

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?

In the paper Efficient Java exception handling in just-in-time compilation, 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:22:38+00:00Added an answer on May 10, 2026 at 9:22 pm

    It depends how exceptions are implemented. The simplest way is using setjmp and longjmp. That means all registers of the CPU are written to the stack (which already takes some time) and possibly some other data needs to be created… all this already happens in the try statement. The throw statement needs to unwind the stack and restore the values of all registers (and possible other values in the VM). So try and throw are equally slow, and that is pretty slow, however if no exception is thrown, exiting the try block takes no time whatsoever in most cases (as everything is put on the stack which cleans up automatically if the method exists).

    Sun and others recognized, that this is possibly suboptimal and of course VMs get faster and faster over the time. There is another way to implement exceptions, which makes try itself lightning fast (actually nothing happens for try at all in general – everything that needs to happen is already done when the class is loaded by the VM) and it makes throw not quite as slow. I don’t know which JVM uses this new, better technique…

    …but are you writing in Java so your code later on only runs on one JVM on one specific system? Since if it may ever run on any other platform or any other JVM version (possibly of any other vendor), who says they also use the fast implementation? The fast one is more complicated than the slow one and not easily possible on all systems. You want to stay portable? Then don’t rely on exceptions being fast.

    It also makes a big difference what you do within a try block. If you open a try block and never call any method from within this try block, the try block will be ultra fast, as the JIT can then actually treat a throw like a simple goto. It neither needs to save stack-state nor does it need to unwind the stack if an exception is thrown (it only needs to jump to the catch handlers). However, this is not what you usually do. Usually you open a try block and then call a method that might throw an exception, right? And even if you just use the try block within your method, what kind of method will this be, that does not call any other method? Will it just calculate a number? Then what for do you need exceptions? There are much more elegant ways to regulate program flow. For pretty much anything else but simple math, you will have to call an external method and this already destroys the advantage of a local try block.

    See the following test code:

    public class Test {     int value;       public int getValue() {         return value;     }      public void reset() {         value = 0;     }      // Calculates without exception     public void method1(int i) {         value = ((value + i) / i) << 1;         // Will never be true         if ((i & 0xFFFFFFF) == 1000000000) {             System.out.println('You'll never see this!');         }     }      // Could in theory throw one, but never will     public void method2(int i) throws Exception {         value = ((value + i) / i) << 1;         // Will never be true         if ((i & 0xFFFFFFF) == 1000000000) {             throw new Exception();         }     }      // This one will regularly throw one     public void method3(int i) throws Exception {         value = ((value + i) / i) << 1;         // i & 1 is equally fast to calculate as i & 0xFFFFFFF; it is both         // an AND operation between two integers. The size of the number plays         // no role. AND on 32 BIT always ANDs all 32 bits         if ((i & 0x1) == 1) {             throw new Exception();         }     }      public static void main(String[] args) {         int i;         long l;         Test t = new Test();          l = System.currentTimeMillis();         t.reset();         for (i = 1; i < 100000000; i++) {             t.method1(i);         }         l = System.currentTimeMillis() - l;         System.out.println(             'method1 took ' + l + ' ms, result was ' + t.getValue()         );          l = System.currentTimeMillis();         t.reset();         for (i = 1; i < 100000000; i++) {             try {                 t.method2(i);             } catch (Exception e) {                 System.out.println('You'll never see this!');             }         }         l = System.currentTimeMillis() - l;         System.out.println(             'method2 took ' + l + ' ms, result was ' + t.getValue()         );          l = System.currentTimeMillis();         t.reset();         for (i = 1; i < 100000000; i++) {             try {                 t.method3(i);             } catch (Exception e) {                 // Do nothing here, as we will get here             }         }         l = System.currentTimeMillis() - l;         System.out.println(             'method3 took ' + l + ' ms, result was ' + t.getValue()         );     } } 

    Result:

    method1 took 972 ms, result was 2 method2 took 1003 ms, result was 2 method3 took 66716 ms, result was 2 

    The slowdown from the try block is too small to rule out confounding factors such as background processes. But the catch block killed everything and made it 66 times slower!

    As I said, the result will not be that bad if you put try/catch and throw all within the same method (method3), but this is a special JIT optimization I would not rely upon. And even when using this optimization, the throw is still pretty slow. So I don’t know what you are trying to do here, but there is definitely a better way of doing it than using try/catch/throw.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 104k
  • Answers 104k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The XmlWriter class is a fast streaming API for XML… May 11, 2026 at 8:34 pm
  • Editorial Team
    Editorial Team added an answer This saves the query defs For i = 0 To… May 11, 2026 at 8:34 pm
  • Editorial Team
    Editorial Team added an answer When …?test is requested, $_GET’s first element’s key will be… May 11, 2026 at 8:34 pm

Related Questions

In Java, is there an elegant way to detect if an exception occurred prior
One of the things that always bugs me about using Readers and Streams in
I was wondering if something exists (in Java world) able to take an snapshot
I'm stuck deciding how to handle exceptions in my application. Much if my issues

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.