consider:

 

class A {

     final Object b;

     public A(C c) {

          try {

               b = c.someMethodThatMayThrowSomeException();

          } catch (SomeException e) {

               b = null; // This line results in compiler error: "variable b might already have been assigned"

          }

     } // take away b=null line and you get "variable b might not have been initialized" on this line

}

 

Why?

How could 'b' be assigned if the exception was thrown?

 

Is this a bug, or am I missing something?

 

Btw, the workaround is to wrap the method call:

 

private final Object initB() {

     try {

          return c.someMethodThatMayThrowSomeException();

     } catch (SomeException e) {

          return null;

     }

}

 

and use b = initB(); in the constructor.  But that seems like it should be unnecessary.

FacebookTwitterLinkedin
Pin It
Joomla Tutorials for Beginners