I've discovered an odd dichotomy in the way Eclipse reports the "The local variable may not have been initialized" error. This error normally occurs if I declare a variable outside a 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(); //Error: The local variable r may not have been initialized This makes sense. I can avoid the error either by initializing the variable to null when I declare it, or by ensuring that the program's control flow never reaches the next statement if an exception occurs inside the try/catch block. Thus, in the case where I really can't continue executing if the variable initialization failed, I can do this:
Random r; try { r = new AESCounterRNG(); } catch (GeneralSecurityException e) { throw new RuntimeException("Initialize secure random number generator failed"); } r.nextInt(); //No error here However, I recently tried using System.exit to stop the program instead of a RuntimeException to make my program's console output cleaner. I would think these are equivalent because both prevent the program from continuing execution, but I discovered that Eclipse does not agree:
Random r; try { r = new AESCounterRNG(); } catch (GeneralSecurityException e) { System.err.println("Couldn't initialize secure random number generator"); System.exit(1); } r.nextInt(); //Error: The local variable r may not have been initialized Why does Eclipse still give me the "not initialized" error when execution could never reach r.nextInt() if an exception occurred? Is this a bug in Eclipse, or is there some way that execution could continue to r.nextInt() even after calling System.exit?
throw new RuntimeException("never happens");afterSystem.exit(1);...this will satisfy the compiler, and at runtime, execution will never reach it, so no harm done.