Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload