0

I have a code like this:

  public void genericOperation(String username, String password) throws AuthFailedException(){
    if(username == null || password == null) throw new NullPointerException();
    AuthMethod(username,password) 
    }

The method AuthMethod check if username or password match, if not will it throws AuthFailedException.

Should I made it a checked exception (AuthFailedException extends Exception) or unchecked (AuthFailedException extends RuntimeException)?

I don't know if the client of this code want to recover the code in case of exceptions.

PiKort
  • 25
  • 3

1 Answers1

1

From the Java documentation on exceptions

Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

In the case of authentication failure, a client can be reasonably expected to take some specific action, such as reprompt for credentials - so this should be a checked exception.

Krease
  • 15,805
  • 8
  • 54
  • 86
  • the problem is that I have no idea what the client want to do in case of failure, what assumption should I choose? – PiKort Nov 29 '18 at 23:48
  • You don't need to know what the client *wants* to do, but whether a client *can* do something to recover from it. Another measure to consider: Is it a *programming* error if there's an exception, or is it possible that it's a *user input* error – Krease Nov 29 '18 at 23:52