Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>We tend to create our most important application specific runtime exception classes with some special constructors, some constants and a ResourceBundle.</p> <p>Example snippet:</p> <pre><code> public class MyException extends RuntimeException { private static final long serialVersionUID = 5224152764776895846L; private static final ResourceBundle MESSAGES; static { MESSAGES = ResourceBundle.getBundle("....MyExceptionMessages"); } public static final String NO_CODE = "unknown"; public static final String PROBLEMCODEONE = "problemCodeOne"; public static final String PROBLEMCODETWO = "problemCodeTwo"; // ... some more self-descriptive problem code constants private String errorCode = NO_CODE; private Object[] parameters = null; // Define some constructors public MyException(String errorCode) { super(); this.errorCode = errorCode; } public MyException(String errorCode, Object[] parameters) { this.errorCode = errorCode; this.parameters = parameters; } public MyException(String errorCode, Throwable cause) { super(cause); this.errorCode = errorCode; } public MyException(String errorCode, Object[] parameters, Throwable cause) { super(cause); this.errorCode = errorCode; this.parameters = parameters; } @Override public String getLocalizedMessage() { if (NO_CODE.equals(errorCode)) { return super.getLocalizedMessage(); } String msg = MESSAGES.getString(errorCode); if(parameters == null) { return msg; } return MessageFormat.format(msg, parameters); } } </code></pre> <p>In the properties file we specify the exception descriptions, e.g.:</p> <pre><code> problemCodeOne=Simple exception message problemCodeTwo=Parameterized exception message for {0} value </code></pre> <p>Using this approach</p> <ul> <li>We can use quite readable and understandable throw clauses (<code>throw new MyException(MyException.PROBLEMCODETWO, new Object[] {parameter}, bthe)</code>)</li> <li>The exception messages are "centralized", can easily maintained and "internationalized"</li> </ul> <p><strong>EDIT:</strong> change <code>getMessage</code> to <code>getLocalizedMessage</code> as Elijah suggested.</p> <p><strong>EDIT2:</strong> Forgot to mention: this approach does not support Locale changing "on-the-fly" but it is intentional (it can be implemented if you need it).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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