ayushgp
ayushgp

Reputation: 5091

Checking whether try was executed or catch was executed in finally

Is it possible to check whether the control handed over to finally came from try or one of the catches? Something like:

try {
    // Some code
} catch(Exception e) {
    // Some more code
} finally {
    // Check if control came from try or from catch?
}

Upvotes: 3

Views: 1074

Answers (3)

Persixty
Persixty

Reputation: 8579

Just as an alternative how about:

try {
    // Some code
} catch(Exception e) {
    // Some more code
    //Code to execute for case where object thrown goes here.
    return; //Maybe return false or re-throw...
} finally{
    //Code to execute regardless of whether exception is thrown.
}
//Code to execute if no exception is thrown...
//return true;//?

This may need to be in a (private) function if the objective is to carry on after the catch case.

I'm just putting this forward because the idea of a finally clause is that it is code that is executed regardless of whether normal or exception-handling control flow takes place and the question is specifically looking for a way to execute different code in those two circumstances. So appears counter the purpose.

We have a pattern for that! It's a catch handler!

Code that is trying to figure that out is a 'code smell'. It may be because the try block is too large and catching a variety of errors which ought to be sub-divided.

As others point out you can use flags to indicate certain points were reached but it's not normally the best approach.

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109547

Better not.

The general pattern is:

try {
    someCodePossiblyRaisingAnException;
    preFinalOkayCode;
} catch (AbcException | DefException e) {
    preFinalFailedCode;
} finally {
    finalCode;
}

Especially using a non-specific Exception is considered bad style by most IDEs and code checkers.

It might be the code using an Exception indicates the desire for a general solution, and that repeated several times. In that case let the caller propagate the exception. For instance with java swing let button actions capture the exception by:

protected ExceptionlessButton { // Or Action
    @Override
    public final void actionPerformed(ActionEvent evt) {
        try {
            onAction(e);
        } catch (Exception e) {
            ...
        }
    }
    abstract protected void onAction(ActionEvent evt);
}

In the presented case the final code seems to belong to either one of the pre-final code parts. When considering try-with-resources that can often even eliminate finally-blocks, you might reconsider the need for such a generalisation.

Result f() throws SQLException {
    try (PreparedStatement stm = ...) {
        someCodePossiblyRaisingAnException;
        preFinalOkayCode;
        return result;
    }
}

Upvotes: 4

NaceurBouhamed
NaceurBouhamed

Reputation: 184

There is no automated way to do it in Java. You can create a boolean flag:

boolean isRaised = false;

try {
    // Some code
}
catch (Exception e) {
    isRaised = true;
}
finally {
    if (isRaised)
        System.out.println("from catch");
    else
        System.out.println("from try");
}

Upvotes: 5

Related Questions