5

Here a simple question :

What do you think of code which use try catch for every instruction ?

void myfunction() {
    try {
        instruction1();
    }
    catch (ExceptionType1 e) {
        // some code
    }
    try {
        instruction2();
    }
    catch (ExceptionType2 e) {
        // some code
    }
    try {
        instruction3();
    }
    catch (ExceptionType3 e) {
        // some code
    }
    try {
        instruction4();
    }
    catch (ExceptionType4 e) {
        // some code
    }

    // etc
}

I know that's horrible, but I want to know if that decreases performance.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
TheFrancisOne
  • 2,667
  • 9
  • 38
  • 58
  • If you want to know if that decreases performance, measure it! There are too many factors to give a definitive answer (your JVM, hardware platform, how time-consuming are those `instructionX` calls, whether exceptions actually get thrown). – NPE Mar 01 '11 at 18:25
  • possible duplicate of [Java try/catch performance, is it recommended to keep what is inside the try clause to a minimum?](http://stackoverflow.com/questions/4280831/java-try-catch-performance-is-it-recommended-to-keep-what-is-inside-the-try-clau) – Gabe Mar 01 '11 at 18:28
  • 1
    The performance decrese is minimal but you could pay a heavy price since you have introduced multiple execution path. If exception 1 gets thrown and you handle it, then exception 2 might get thrown. It actually depends on your business logic ( should the execution break if any of the exception gets thrown or not). But performance is not an issue with your code. – uncaught_exceptions Mar 01 '11 at 18:31
  • your code sample is misleading... What you refer to as an "instruction" is written as if it was always a method call. Did you meant *"What do you think of code which use try/catch around every method call?"* Because I'd for sure have even more things to say about wrapping instructions like *int a = 42;* in a *try/catch* ;) – SyntaxT3rr0r Mar 01 '11 at 20:09
  • This will never create a noticeable decrease in performance – SARose Jan 24 '15 at 04:56

5 Answers5

9

Try something like this: (I know this doesn't answer your question, but it's cleaner)

void myfunction() {
try {
    instruction1();
    instruction2();
    instruction3();
    instruction4();
}
catch (ExceptionType1 e) {
    // some code
}
catch (ExceptionType2 e) {
    // some code
}
catch (ExceptionType3 e) {
    // some code
}
catch (ExceptionType4 e) {
    // some code
}

// etc
}
Richard Pianka
  • 3,317
  • 2
  • 28
  • 36
6

It doesn't decrease performance (certainly not noticeably) but it does indeed look horrible.

Remember the golden rule: readable code is often faster code. Produce readable code first and only change it if it proves to be too slow.

Group your try-catch blocks into logical chunks of the original code (e.g. one for opening a stream and reading data from it, one for all the processing done afterwards) and your maintenance developers will thank you for it.

You should also consider that if your first catch block catching an exception doesn't end abruptly (that is, execution continues in the normal flow), additional exceptions might be thrown by subsequent instructions that rely on the previous ones completing successfully, which are both spurious and they can slow your code down.

biziclop
  • 48,926
  • 12
  • 77
  • 104
5

No, this probably doesn't decrease performance (but, as always, you'd have to test it). When compiled into bytecode, the Java exception handlers are changed into a table of bytecode offset ranges and associated handler addresses. The catch handlers are generally compiled outside the mainline of the code, so do not affect "normal" non-exception execution.

More information can be found in The Java Virtual Machine Specification, section 7.12 Throwing and Handling Exceptions. This section also provides examples of how various code constructs might be compiled to bytecode.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
2

Using excessive amounts of try-catch can be start to be expensive in terms of performance if exceptions are thrown. You would be better off either not having so many checked exceptions or having all your ExceptionTypes extend a common Exception base class. This way you can have a single try catch which is much easier to read and has better performance.

Ian Dallas
  • 12,451
  • 19
  • 58
  • 82
1

As you can see from this answer, the performance hit it minimal unless an error is thrown.

Community
  • 1
  • 1
FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82