开发者

What exception to throw when pre-requisite method was not called?

开发者 https://www.devze.com 2023-03-17 01:53 出处:网络
I have a method getUser which retrieves a user from a database. The method demands that you verify that the user actually exists (via the userExists(String username) method.

I have a method getUser which retrieves a user from a database. The method demands that you verify that the user actually exists (via the userExists(String username) method.

If the getUser method is invoked and the user does no开发者_如何学Pythont exist, I want to throw an unchecked exception, but which exception is the most appropriate here? I thought about IllegalArgumentException, but it doesn't feel completely right, as certain inputs may be okay in some cases, but not others - they are not strictly "illegal". Any suggestions?


To me IllegalArgumentException means the argument is illegal and would always to illegal. Th exception I would use is IllegalStateException to say the state of the object to check the user is not valid.

However you may have an exception which is specific enough you could create your own.

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

This may make debugging things easier.

A NumberFormatException is a subclass of IllegalArgumentException. Ifyou try to parse the number 12QW4 it will give you a NumberFormatException and there is nothing you can do to make this a valid argument later. i.e. it has nothing to do with the state of anything.

The Javadoc for IllegalStateException states.

Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.


IllegalStateException is not the right choice. IllegalStateException refers to the state of the object whose method has been called - ie the state of this is incorrect to be called as it has been. In your case this (ie the database server) is just fine, it's the user that is "incorrect".

IllegalArgumentException is the right choice - it's the user that is incorrect, not the server.

The other valid option is throwing a you own domain-specific exception, such as UnknownUserException, but if this case is "rare" or unexpected and unrecoverable, it's OK stick with IllegalArgumentException


I wouldn't throw any exception, and simply return null. Or I would throw a functional exception called UserDoesNotExistException.

Here's my reasoning: IllegalStateException is used when the user calls a method which is forbidden given the state of the object. Here, it's not the state of the object which causes the exception. It's the fact that the user doesn't exist in database.

You might argue that the user should have called userExists before, and that this method could even remember, using an instance field, that it has been called (with this argument), so that an IllegalStateException can be thrown by getUser without even going to the database.

But the problem here is that calling userExists before probably doesn't add anything: it will perform a query to check that a user exists, then getUser will perform a second query, and is not guaranteed to find the user, since another transaction might have removed it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号