I found a strange dichotomy in the way Eclipse reports the error "The local variable may not have been initialized." This error usually occurs if I declare a variable outside the try / catch block, initialize it inside the try / catch block, and then use it after the try / catch block:
Random r; try { r = new AESCounterRNG(); } catch (GeneralSecurityException e) { e.printStackTrace(); } r.nextInt();
It makes sense. I can avoid the error either by initializing the variable to null when I declare it, or by ensuring that the program control flow never reaches the next statement if an exception occurs in the try / catch block. Thus, in the case where I really can not continue execution, if the variable initialization fails, I can do this:
Random r; try { r = new AESCounterRNG(); } catch (GeneralSecurityException e) { throw new RuntimeException("Couldn't initialize secure random number generator"); } r.nextInt();
However, I recently tried using System.exit to stop a program instead of a RuntimeException to make my program console cleaner. I think they are equivalent because both prevent the program from continuing, but I found that Eclipse disagrees:
Random r; try { r = new AESCounterRNG(); } catch (GeneralSecurityException e) { System.err.println("Couldn't initialize secure random number generator"); System.exit(1); } r.nextInt();
Why does Eclipse still give me a โnon-initializedโ error when execution cannot reach r.nextInt() if an exception r.nextInt() ? Is this a bug in Eclipse, or is there a way that execution can continue until r.nextInt() even after calling System.exit ?
source share