commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ole Ersoy <>
Subject [math] Exception Design
Date Mon, 21 Dec 2015 18:14:16 GMT

I was considering jumping into the JDKRandomGenerator exception discussion, but I did not
want to hijack it.

Not sure if any of you have had a chance to looks at this:

I think it satisfies everyone's requirements with:
- A single MathException (No hierarchy)
- The ExceptionTypes Enum contains all the exception types
- The ExceptionTypes Enum 'key' maps to the corresponding message 1 to 1
- The ExceptionFactory (Utility) throws exceptions, if necessary, that have always have a
single unique root cause, such as NPEs
- The context captures the exception parameters keyed by an the 'ExceptionKeys' enum.  Each
module can introduce more keys as necessary.
- The toString() method can be used as the initial exception message

The way developers should deal with this exception is:
1) Catch
2) Get the type (Enum)
3) Get the  parameters (Context)
4) Get the method that threw it
5) Get the class threw it
6) Rethrow the above in an application specific exception, log it, or display it.  Construct
a localized message using the enum type to look up the exception template if needed.


- Ole

P.S. Here's the entire test demo (Pasted below for convenience):

   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.

package com.fireflysemantics.math.exceptions;

import static com.fireflysemantics.math.exception.ExceptionKeys.CONSTRAINT;
import static com.fireflysemantics.math.exception.ExceptionKeys.VALUE;
import static com.fireflysemantics.math.exception.ExceptionTypes.NUMBER_TOO_SMALL;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import com.fireflysemantics.math.exception.ExceptionFactory;
import com.fireflysemantics.math.exception.MathException;

public class MathExceptionTest {

     @Test(expected = MathException.class)
     public void verifyThrows() {
         throw new MathException(NUMBER_TOO_SMALL);

     public void verifyCode() {
         try {
             throw new MathException(NUMBER_TOO_SMALL);
         } catch (MathException e) {
             assertEquals(e.getType(), NUMBER_TOO_SMALL);

     public void verifyContext() {
         try {
             throw new MathException(NUMBER_TOO_SMALL).put(CONSTRAINT, 2).put(VALUE, 1);
         } catch (MathException e) {
             assertEquals(e.get(CONSTRAINT), 2);
             assertEquals(e.get(VALUE), 1);

     public void verifyToString() {
         try {
             throw new MathException(NUMBER_TOO_SMALL).put(CONSTRAINT, 2).put(VALUE, 1);
         } catch (MathException e) {

     public void verifyFactory() {
         try {
             ExceptionFactory.throwNumberToSmallException(1, 2, "foo");
         } catch (MathException e) {
             assertTrue(e.getType() == NUMBER_TOO_SMALL);
             assertEquals(e.get(CONSTRAINT), new Integer(2));
             assertEquals(e.get(VALUE), new Integer(1));
             assertEquals(e.get("foo"), new Integer(1));

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message