TheFrancisOne
TheFrancisOne

Reputation: 2737

Java try catch blocks

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.

Upvotes: 5

Views: 8430

Answers (5)

Ian Dallas
Ian Dallas

Reputation: 12741

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.

Upvotes: 2

biziclop
biziclop

Reputation: 49804

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.

Upvotes: 6

Richard Pianka
Richard Pianka

Reputation: 3366

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
}

Upvotes: 9

FreeAsInBeer
FreeAsInBeer

Reputation: 12979

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

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 994797

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.

Upvotes: 5

Related Questions