What is the exception to throw when the preprocessing method has not been called?

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).

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?

+5
source share
3 answers

IllegalArgumentException , . IllegalStateException, , .

, , .

public class UsernameNotCheckedException extends IllegalStateException {
    public UsernameNotCheckedException(String message) {
        super(message);
    }
}

.

A NumberFormatException IllegalArgumentException. 12QW4, NumberFormatException, . -.

Javadoc IllegalStateException.

, . , Java Java .

+11

IllegalStateException . IllegalStateException , , .. this , , . this (.. ) , "".

IllegalArgumentException - - , .

- , UnknownUserException, "" , IllegalArgumentException

+3

null. UserDoesNotExistException.

: IllegalStateException , , . , . , .

, userExists , , , ( ), IllegalStateException getUser, .

, userExists , , : , , , getUser , .

+1
source

All Articles