That is because you are supplying it. You are passing e.getMessage()
to your constructor as the only argument, customMessage. You then pass customMessage
to its parent's constructor that takes a String
, Exception(String)
. In doing so, you are giving it the message to use for serviceExceptionInstance.getMessage()
. Instead, do not pass the customMessage
to its parent (use super();
, which is implied if no call to a parent constructor is given and a no-arg, parent constructor exists). Then the message will be null
as it is not supplied.
In other words:
new ServiceException(e.getMessage());
Creates a new ServiceException
with the message from e
. You pass that message to Exception
, ServiceException
's parent.
super(customMessage);
In doing so, you use the single argument, String
-based constructor of Exception
. Passing a value to that constructor implies that you want it used when callers invoke getMessage
. To avoid doing this, call a different parent constructor, or none at all (calling none is technically not possible, and it will implicitly do super();
for you):
public ServiceException(String customMessage)
{
this.customMessage = customMessage;
}
This will call super();
for you, which means that the parent class has no message to send, and by not overriding getMessage()
yourself, then it will return its default value (null
). To be clear, Exception
itself extends from Throwable
, which is really the class providing this functionality, but it all stems from how you work with Exception
as it serves as a pass-thru to Throwable
.