Inside my Java code, it is checking for !null
condition and throwing an Exception
.
For example
try { if (stud.getCall() != null) acc.Call = stud.getCall().toString(); else throw new Exception("Data is null"); } catch (Exception e) { logger.error("Some Error" + e.getMessage()); throw new Exception("Please check the Manatadatory Field is Missing" + e.getMessage()); }
But in the logs I am getting:
Some Error null
Why is the e.getMessage
null
?
You are catching a different exception to the one that your code is explicitly creating and throwing1. The exception that you are catching doesn't have a message. You need to log the entire exception, not just the exception's message.
The getMessage() method of Throwable class is used to return a detailed message of the Throwable object which can also be null. One can use this method to get the detail message of exception as a string value. Syntax: public String getMessage()
NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.
The GetMessage function retrieves messages associated with the window identified by the hWnd parameter or any of its children, as specified by the IsChild function, and within the range of message values given by the wMsgFilterMin and wMsgFilterMax parameters.
You are catching a different exception to the one that your code is explicitly creating and throwing1. The exception that you are catching doesn't have a message.
You need to log the entire exception, not just the exception's message. Among other things, that would tell you what the caught exception's actual class is and where the exception was created/thrown.
Based on the fact that the exception doesn't have a message, I'd guess that it is an NPE caused by stud
or acc
being null, or by stud.getCall()
returning null
... or something like that. A NullPointerException
generated natively (i.e. by the JVM) has a null
message2.
java.lang.Exception
is Bad PracticeYour problem illustrates why it is generally speaking a bad idea to create/throw Exception
When you throw Exception
it becomes next to impossible to discriminate between it and other (unexpected) exceptions in a catch
clause. And that is what has happened here: you've caught the wrong exception.
You should pick a more specific exception, and if no appropriate one exists, implement one of your own.
1 - You could use e.printStackTrace()
, a logger call, or a debugger to see which exception you have actually caught.
2 - This is not true on Android. There, an NPE has a helpful message that gives contextual information. It is also no longer true in Java 14 and later; see https://openjdk.java.net/projects/jdk/14/ and https://openjdk.java.net/jeps/358
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With