11

I have a very silly question for you :)

For example, I have following code snippet:

class MyClass {

    public static void main (String[] args) {

        final String status;

        try {
            method1();
            method2();
            method3();
            status = "OK";
        } catch (Exception e) {
            status = "BAD"; // <-- why compiler complains about this line??
        }

    }

    public static void method1() throws Exception {
        // ...
    }

    public static void method2() throws Exception {
        // ...
    }

    public static void method3() throws Exception {
        // ...
    }

}

The question is inside: why compiler complains about this line?

IntelliJ IDEA says, that Variable 'status' might already have been assigned to.

But, as I can see, we don't ever reach line (where we set status = "OK") in case of exceptional situation. So the status variable will be BAD and everything should be ok. And if we don't have any exception, then we get OK. And we will set this variable only ONCE a time.

Any thoughts about this?

Thanks for your help!

1
  • 1
    compiler is not that smart, and if you are declaring that method could throw an exception compiler wont be struggling to check it, so for the sake of simplicity it does not allow such things to happen Commented Jun 29, 2015 at 18:47

2 Answers 2

12

The Java compiler doesn't see what you and I see -- that either status gets set to "OK" or it gets set to "BAD". It assumes that status can be set and an exception is thrown, in which case it gets assigned twice, and the compiler generates an error.

To workaround this, assign a temporary variable for the try-catch block, and assign the final variable once afterwards.

final String status;
String temp;

try {
    method1();
    method2();
    method3();
    temp = "OK";
} catch (Exception e) {
    temp = "BAD";
}

status = temp;
Sign up to request clarification or add additional context in comments.

4 Comments

I think it is still valid error, what if this thread gets interrupted and throws InterruptedException right after assignment
@JigarJoshi It can't throw InterruptedException (maybe something else, but not that, and probably not in this code).
if this was in separate thread and that got called on interrupt()
@JigarJoshi Then the interrupt flag would be set on the Thread. An InterruptedException will only happen if the thread is blocked on IO (or something similar: locks, etc.).
2

What if the code that caused the exception occured after status = "OK"? The reason you get the error seems pretty obvious.

Take this for example:

final String status;

try {
    status = "OK":
    causeException();
}catch(Exception e) {
    status = "BAD";
}

void causeException() throws Exception() {
    throw new Exception();
}

This would result in reassigning the variable, which is why you get an error.

2 Comments

The variable is final. Reassigning is forbidden.
@swinkler That's the point I'm getting across. The error occurs because the code shown above is possible. Man, what's up with StackOverflow now-a-days..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.