Checked exceptions should only be used if the exception is unavoidable with proper use of the API and the API user can take some useful action when the exception occurs.
If only one checked exception is thrown by the method, does it substantially increase the burden on the implementer? .. (** I couldn't understand what the theory was **)
Instead of throwing a checked exception, there is a way to return an empty Optional. The disadvantage of this method of returning Optional is that it cannot deliver information to the caller about the failure to perform the desired operation.
As a means of avoiding a checked exception throw, there is a method that returns a boolean that checks the state and a method that returns an unchecked exception, although it does not always hold.
// Invocation with state-testing method and unchecked exception
if (obj.actionPermitted(args)) {
obj.action(args);
} else {
... // Handle exceptional condition
}
Recommended Posts