How to create and use Exceptions having custom attributes. Also how to format the message using the attributes.

This is a very common requirement, specially in validation frameworks to create new Exceptions with attributes. Often we need to get the message using the attributes later.

import java.text.MessageFormat;

public class MyException extendsException {   
    private final String message_;
    private final String myAttr1_;
    private final String myAttr2_;

    public MyException() {
        super();
        myAttr1_ = null;
        myAttr2_ = null;
        message_ = null;
    }
    public MyException(String myAttr1,String myAttr2 ) {
        super();
        myAttr1_ = myAttr1;
        myAttr2_ = myAttr2;
        message_ = null;
    }
    public MyException(String myAttr1) {
        super();
        myAttr1_ = myAttr1;
        myAttr2_ = null;
        message_ = null;
    }
   
    /* (non-Javadoc)
     * @see java.lang.Throwable#getMessage()
     */
     // We have to overide getMessage() to form the message using custom attributes.

    public String getMessage() {
        String message;       
        String messageFormat = getMessageFormat();
        if (null == messageFormat) {
            message = super.getMessage();
        }
        else {
            Object[] args = getMessageArgs();
            message = MessageFormat.format(messageFormat, args);
        }       
        return message;
    }
   
    // this is a utility method
    protected Object[] getMessageArgs() {
        Object[] args = { myAttr1_,  //0
            myAttr2_  //1
        };
        return args;
    }
    
    // this is a utility method
    protected String getMessageFormat() {
        String message;
        if (message_ != null) {
            message = message_;
        }
        else {
            message = "An Exception Raised!! Exception Arg1:  " +
                    "\"{0}\" , Exception Arg2: \"{1}\".";
        }
        return message;
    }
}

 


"Any fool can make things bigger, more complex, and more violent. It takes a touch of genius-and a lot of courage-to move in the opposite direction."

"The only reason for time is so that everything doesn't happen at once."

"Gravitation is not responsible for people falling in love."

- Albert Einstein

Following is a simple test class to test our custom exception with attributes;
Test Class:

public class MyExceptionTest {   
    public static void main(String[] args) {       
        MyExceptionTest myExceptionTest = new MyExceptionTest();
        try{
            myExceptionTest.test1();  
        }catch(MyException e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
       
        try{
            myExceptionTest.test2();  
        }catch(MyException e){
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }   
    public void test1() throws MyException{       
        // simply throw our new exception here   
        throw new MyException("MyExcepAttr1");       
    }
   
    public void test2() throws MyException{      
         // simply throw our new exception here    
        throw new MyException("MyExcepAttr1", "MyExcepAttr2");       
    }
}

Test Output:

An Exception Raised!! Exception Arg1:  "MyExcepAttr1" , Exception Arg2: "null".
An Exception Raised!! Exception Arg1:  "MyExcepAttr1" , Exception Arg2: "MyExcepAttr2".