I have a method getUserthat retrieves a user from a database. This method requires confirmation that the user really exists (using the method userExists(String username).
getUser
userExists(String username)
If the method getUseris called and the user does not exist, I want to throw an unchecked exception, but which exception is most suitable here? I thought about IllegalArgumentException, but it doesn’t feel completely right, because in some cases some inputs may be OK, but not others - they are not strictly “illegal”. Any suggestions?
IllegalArgumentException
IllegalArgumentException , . IllegalStateException, , .
, , .
public class UsernameNotCheckedException extends IllegalStateException { public UsernameNotCheckedException(String message) { super(message); } }
.
A NumberFormatException IllegalArgumentException. 12QW4, NumberFormatException, . -.
NumberFormatException
12QW4
Javadoc IllegalStateException.
IllegalStateException
, . , Java Java .
IllegalStateException . IllegalStateException , , .. this , , . this (.. ) , "".
this
IllegalArgumentException - - , .
- , UnknownUserException, "" , IllegalArgumentException
UnknownUserException
null. UserDoesNotExistException.
null
UserDoesNotExistException
: IllegalStateException , , . , . , .
, userExists , , , ( ), IllegalStateException getUser, .
userExists
, userExists , , : , , , getUser , .