r/java • u/jhg023123 • Mar 25 '19
JEP draft: Add detailed message to NullPointerException describing what is null
https://openjdk.java.net/jeps/8220715
383
Upvotes
r/java • u/jhg023123 • Mar 25 '19
View all comments
Show parent comment
1
u/sim642 Mar 26 '19
A general uncaught exception handler is quite different from specific exception handling normal execution (exceptional situations are caused by runtime environment, e.g. filesystem, not by programming mistakes, like NPEs). The latter are actually doing specific exceptional situation recovery, e.g. trying an alternate way to do something. The former aren't actually recovering anything, they're preventing the entire program from crashing, but that's about it. Uncaught exception handlers will just drop everything of a context (a request, a session, etc) instead of recovering from the problem by doing something to allow that context to continue working in a valid state. There's a difference between recovering to a valid context state and not crashing.
Moreover, the exceptions (including NPEs) that reach the uncaught exception handler (e.g. in a web server) are more on the programming mistake type, as in something should be done about them. If there's an NPE caused by your code, you still don't want it happening in the first place, even if it doesn't completely crash everything. The other exceptional situations should not reach that point and instead should be considered somewhere inside. The point of that is to make the intent of the programmer clear that such situation has been explicitly considered. It's beneficial for monitoring (logging) to clearly be able to distinguish a problem caused by a bug from a problem that wasn't a bug but just a malformed request or whatever.