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?
-
3Even java.lang.Exception (http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Exception.html) has a constructor with a String. – anubhava Jan 17 '12 at 13:10
8 Answers
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.

- 486,780
- 108
- 951
- 1,012
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;
}
}

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

- 2,617
- 1
- 17
- 31
The best approach is to wrap the exception.
try {
my code that throws E;
} catch (final E e) {
throw new MyE("my message", e);
}

- 2,939
- 19
- 17
The Exception class (its parent, actually - Throwable) takes a message as an argument in its constructor:
throw new Exception("message")
or Exception("message", innerException);

- 30,738
- 21
- 105
- 131

- 101
- 1
- 6
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.

- 30,738
- 21
- 105
- 131

- 7,538
- 1
- 40
- 64
Try this code:
try{
throw new Exception("Test String");
}
catch(Exception ex){
System.out.println(ex.getMessage());
}

- 139
- 7
-
1An 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? – Peter Mortensen Sep 01 '20 at 00:52
Another variant
public class MyCustomExceptionWithoutMessageInConstructor extends IllegalArgumentException {
private static final String ERROR_MESSAGE = "terrible error";
public MyCustomExceptionWithoutMessageInConstructor() {
super(ERROR_MESSAGE);
}
}

- 174
- 1
- 14