The reason for first code is:
Whenever you throw an exception (without any condition) - obviously, the next line from same scope is not executed. Thus, here, compiler gives unreachable code error (because there is guarantee that next line will never be reached).
For second code - from compiler's POV, it is simply a method call. It seems that compiler is not intelligent enough to 'decode' the meaning of System.exit at compile time (what if you use some custom classloader which loads your own 'System' class - which is having an 'exit' method - but it does not actually exits the JVM?). Thus, compiler will not give unreachable code error.
The compiler knows what throwing an exception means, but it has no idea what side effects calling System.exit has. As far as the compiler is concerned, it's a method call like any other, and code execution will resume at the next statement after it.
Edit: ... what he said :-)
posted 6 years ago
Thanks for the answers. Good reasons. It helps me to understand in more detail the compiler.
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop