简体   繁体   中英

How can I set the message on an exception in Java?

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?

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.

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;
    }

}

好吧,如果 API 提供了适合您需要的异常(例如 IllegalArgumentException),只需使用它并在构造函数中传递您的消息。

The best approach is to wrap the exception.

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

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

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

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.

Try this code:

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

Another variant

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM