27

I want to set a custom exception message. However, I'm unsure of how to do this. Will I need to create a custom exception class or is there an easier way of doing this?

1

8 Answers 8

41

Most standard exception classes provide a constructor that takes a mesage, for example:

public UnsupportedOperationException(String message) {
    super(message);
}

The above class simply calls its parent's constructor, which calls its parent's constructor, and so on, ultimately culminating in:

public Throwable(String message) {
    ...
}

If you create your own exception class, I think it's a good idea to following this convention.

Sign up to request clarification or add additional context in comments.

Comments

24

You can only set the message at the creation of the exception. Here is an example if you want to set it after the creation.

public class BusinessException extends RuntimeException{

    private Collection<String> messages;

    public BusinessException(String msg){
        super(msg);
    }


    public BusinessException(String msg, Exception cause){
        super(msg, cause);
    }


    public BusinessException(Collection<String> messages){
        super();
        this.messages= messages;
    }


    public BusinessException (Collection<String> messages, Exception cause){
        super(cause);
        this.messages= messages;
    }

    @Override
    public String getMessage(){
        String msg;

        if(this.messages!=null && !this.messages.isEmpty()){
            msg="[";

            for(String message : this.messages){
                msg+=message+",";
            }

            msg= StringUtils.removeEnd(msg, ",")+"]";

        }else msg= super.getMessage();

        return msg;
    }

}

Comments

15

Well, if the API offers an exception that suits your needs (IllegalArgumentException for example), just use it and pass your message in the constructor.

Comments

4

The Exception class (its parent, actually - Throwable) takes a message as an argument in its constructor:

throw new Exception("message") or Exception("message", innerException);

Comments

3

The best approach is to wrap the exception.

try {
    my code that throws E;
} catch (final E e) {
    throw new MyE("my message", e);
}

Comments

2

The root Exception class accepts a String custom message, as do (as far as I can tell) all of derivative classes.

So: no, you don't need to create a custom class. One of the existing exceptions probably covers your case (read their descriptions to find out which). If none of those are really satisfactory, then you can create an extension of Exception (or RuntimeException, etc.) and maintain the custom message constructor.

Comments

1

Try this code:

try{
    throw new Exception("Test String");
}
catch(Exception ex){
    System.out.println(ex.getMessage());
}

1 Comment

An explanation would be in order. Especially as it is very different from the other answers, it would be nice to see a justification for if it really answers the question. For example, what is the idea behind it?
0

Another variant

public class MyCustomExceptionWithoutMessageInConstructor extends IllegalArgumentException {
    private static final String ERROR_MESSAGE = "terrible error";

    public MyCustomExceptionWithoutMessageInConstructor() {
        super(ERROR_MESSAGE);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.