3

The question says it all. Although the hit is not highly significant (I measured it to be between 1.5x to 2x slower), there's no difference between the byte code with try-catch and the byte code without it. So what makes it generally slower?

Pl. note that the question is not about overhead of throwing an exception, but of entering/leaving a try block.

EDIT: here's the code (run on Hotspot 1.6.0_31 server)

static void tryCatch()
{
    int i = 0;
    long l1 = getTime();
    for(int j = 0; j < 100000; j++)
    {
        try
        {
            i++;                
        }
        catch(Exception e)
        {

        }
    }
    long l2 = getTime();
    System.out.println("with try-catch: " + (l2 - l1) + ": " + i);      
}

static void noTryCatch()
{
    int i = 0;
    long l1 = getTime();
    for(int j = 0; j < 100000; j++)
    {
        i++;
    }
    long l2 = getTime();
    System.out.println("w/o  try-catch: " + (l2 - l1) + ": " + i);
}

static long getTime()
{
    return System.nanoTime();       
}
Vadzim
  • 24,954
  • 11
  • 143
  • 151
shrini1000
  • 7,038
  • 12
  • 59
  • 99
  • 12
    Please show how you measured this. Benchmarking is notoriously difficult to get right. – Jon Skeet Apr 16 '12 at 06:39
  • 3
    Also: try-catch is not only represented in the bytecode, but in code attributes of the method. In other words: you only see *part* of the try-catch implementation in the byte code. – Joachim Sauer Apr 16 '12 at 06:41
  • 1
    Here's the question that probably has the answer you seek. Not sure if this can be considered duplicate question: http://stackoverflow.com/questions/2633834/should-java-try-blocks-be-scoped-as-tightly-as-possible – bezmax Apr 16 '12 at 06:43
  • @Hassan, agreed; but if try-catch is used sparingly, it won't affect the overall performance significantly. The hit I showed is seen when you execute it in long loops. – shrini1000 Apr 16 '12 at 06:48
  • @Joachim Sauer: thanks! Could you pl. elaborate on it? – shrini1000 Apr 16 '12 at 06:57
  • I have replaced your println with return (l2-l1) and run each 10000000x in a method. The result now is that tryCatch() is faster. :) – edze Apr 16 '12 at 07:13
  • If you aren't printing the value of local 'i' variable within the same method, maybe the JIT is optimizing it away? The result might vary if you make it a static variable and print it at the end of each loop? – shrini1000 Apr 16 '12 at 07:22
  • Possible duplicate of [Is it expensive to use try-catch blocks even if an exception is never thrown?](https://stackoverflow.com/questions/16451777/is-it-expensive-to-use-try-catch-blocks-even-if-an-exception-is-never-thrown) – Vadzim Jun 04 '19 at 16:14

2 Answers2

9

Since you have a micro-benchmark its is more likely you are testing how confusing the try/catch block is to the JVM compiler. For example, the JVM can be smart enough to change

for(int j = 0; j < 100000; j++) {
    i++;
}

into

i += 100000 * 1;

using the try/catch block may prevent the more aggresive optimisations, but might not make any difference for a more realistic block of code.


In any case I would normally change something like

for(int j = 0; j < 100000; j++) {
    try {
        // do something
    } catch(Exception e) {
        // break or return
    }
}

.

try {
    for(int j = 0; j < 100000; j++) {
        // do something
    }
} catch(Exception e) {
    // continue or return
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thx (and +1) for a detailed answer. But I want to measure the cost of 'entering' a try block. In the approach you've suggested, I probably won't be able to measure it. – shrini1000 Apr 16 '12 at 07:26
  • I believe the cost is only that it might prevent a micro-optimisation of the JVM by making the code more complex. e.g. methods are typically only inlined if they are 35 bytes or less. a try/catch block could add a few bytes which could prevent inlining. – Peter Lawrey Apr 16 '12 at 07:29
2

My microbenchmark for another question showed there is no significant difference between using/not using a try-catch block, with or without throwing exception in a block or not.

Community
  • 1
  • 1
Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68