Condition always false?

Throwable throwable = null;
try {
connThread.start();
} catch (Throwable t) {
throwable = t;
} finally {
if (throwable != null) {
setState(STATE_FAILED, throwable);
}
}

IDEA underlines "throwable != null" and says that it's always false (that is, throwable is always null). That isn't true, is it? Is there something I don't understand about try/catch/finally?

7 comments
Comment actions Permalink

why don't you rewrite it like this? It is much clearer IMHO and it eliminates a finally clause.


try {
    connThread.start();
} catch (Throwable t) {
     setState(STATE_FAILED, t);
}

0
Comment actions Permalink

I don't believe the code in the catch is guaranteed to be called. I might be wrong there too, though.

0
Comment actions Permalink

Code in a catch is not guaranteed to be called like a finally block, but since you only want setState(x,x) called when an exception is thrown it make more sense to be in the catch.

0
Comment actions Permalink

Regardless of the quality of the code. The question is Why is Idea asserting that in the if statement, throwable is always null?

I don't think it should be making such an assertion, since if the catch clause is triggered the throwable will be assigned a value.

However - that being said - I've just inserted the sample code into my copy of Idea (876) and cannot reproduce the behaviour.

0
Comment actions Permalink

You're right, now that I look at it again. That is pretty dumb code. :) I'm hoping that was a remnant of old code where the code I pasted made sense, and not that I'm just dumb. :) But I still want to know why IDEA says that.

0
Comment actions Permalink

This is in 873. I haven't installed 876 because I heard it has some problems, but I'll test it when I get the next build, I guess.

0
Comment actions Permalink

This example isn't very good, but Idea should pick up assignments in catch blocks...IMHO

0

Please sign in to leave a comment.