Eclipse does not consider System.exit to abort

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(); //Error: The local variable r may not have been initialized 

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(); //No error here 

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(); //Error: The local variable r may not have been initialized 

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 ?

+4
source share
2 answers

Good question, he also listened to me several times.

The problem is that unlike an exception exception, just calling the method (and the fact that System.exit(1); ) does not guarantee at all that the program flow will stop. Of course, the System.exit () documentation says that this method never returns normally. But although the semantics of throw determined by the language itself, the semantics of System.exit() is found only in Javadocs.

I assume that they simply did not bother to realize this special case. Although there is an error message in which this topic is addressed ( https://bugs.eclipse.org/bugs/show_bug.cgi?id=126551 , see Comment 2), it is marked as โ€œwont fixโ€ because it seems too complicated.

Edit: As rlegendi pointed out, this is actually a Java compiler issue, not just Eclipse. My solution so far has been to simply go with a simple old throw (instead of the special throw() method), which (otherwise, is a very small application) is anyway better than System.exit() .

+5
source

This is not an error: in the second example, it is guaranteed that r initialized on the call site (you throw an exception otherwise, so that the execution branch is closed).

In the first and third examples, you simply execute the program code and leave r undefined. If you set it to null either in the exception handling block or in the declaration, it will not complain.

Btw is not an Eclipse problem, it is determined by JLS that you cannot use an uninitialized variable. Try compiling it using Java and you will get exactly the same result.

+1
source

Source: https://habr.com/ru/post/1435684/


All Articles