You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Exception class must have at least one constructor with signature public Constructor(Throwable, String, Object...) or public Constructor(String, Object...)#31
When we are going to throw the custom exceptions, we have to assemble the message, such constructors in exceptions will make code cleaner:
classMyExceptionextendsException {
/** * The constructor that supports exception message assembling. * The arguments might be omitted. * * @pattern The exception message pattern. * @args The arguments to assemble the exception message * @see String#format */publicMyException(Stringpattern, Object... args) {
super(String.format(pattern, args));
}
/** * The constructor that supports exception message assembling. * The arguments might be omitted. * * @pattern The exception message pattern. * @args The arguments to assemble the exception message * @see String#format */publicMyException(Throwablecause, Stringpattern, Object... args) {
super(String.format(pattern, args), cause);
}
}
and use case will be
thrownewMyException(
"Unable to process order '%s' for '%s' at '%s'",
orderId, userId, System.getProperty("server.name")
);
// ortry {
...
} catch(SomeExceptioncause) {
thrownewMyException(
cause, "Unable to process order '%s' for '%s' at '%s'",
orderId, userId, System.getProperty("server.name")
);
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Summary
Why?
When we are going to throw the custom exceptions, we have to assemble the message, such constructors in exceptions will make code cleaner:
and use case will be
Beta Was this translation helpful? Give feedback.
All reactions