Your first example will work if you only intend to use throw new MyCustomException()
, but in case of exceptions... you might want to provide more information. It can make debugging easier.
If someone else is to use your exception class, they will probably expect to be able to use more explicit messages, or pass it a cause in case they're rethrowing. Or if someone else has to debug your code, your chance of not being strangled is much better if it doesn't throw exceptions without message or cause.
For example a custom exception I used in my last project,
public final class CryptorException extends Exception {
private static final long serialVersionUID = 1L;
public CryptorException(String message) {
super(message);
}
public CryptorException(Throwable cause) {
super(cause);
}
public CryptorException(String message, Throwable cause) {
super(message, cause);
}
}
That's all. It takes a couple of seconds and very little effort. Note though how I didn't implement a constructor without parameter. If you want it, you'll need to add it.
Technically, the constructors don't override anything because constructors are not inherited to begin with.
For an explanation of the serialVersionUID, see this question. If you're not serializing anything, you don't need to care about it.