Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Singleton exceptions are supposed to be a performance optimization. The thinking is that you eliminate the cost of populating the stack trace when creating an exception which is usually the most expensive part of exceptions.</p> <p>If the exception were sufficiently unique and descriptive and the stacktrace was not going to be used, then a singleton could potentially be effective. You could design the app such that a specific exception type with a specific message always means that an exception orgininated from a specific location. Then the stack trace would be irrelevant.</p> <p>The <a href="http://java.sun.com/developer/JDCTechTips/2003/tt0422.html#2" rel="noreferrer">April 22, 2003 Tech Tips article</a> describes a scenario where you reuse an exception. In this case, they are trying to game the garbage collector by reducing the number of objects created. If you skipped the <code>populateStackTrace()</code> call, you would not have to worry about threading.</p> <p>Generally, if the performance impact of an exception is causing problems, that is a sign that exceptions are being used for application logic and an error-code should be used instead.</p> <p>In newer JVM's (1.4+, I believe), this "optimization" can be done automatically by the JVM when running in "-server" mode. This hotspot optimization can be controlled by the option <code>-XX:+OmitStackTraceInFastThrow</code>.</p> <p><em>Personally, I would recommend against using the singleton exception [anti-]pattern.</em></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