0

I am coding in java since long, However I am confused in Exception Handling. I have to check the valid user.

One way is the if the user is not in db throw no user found exception and then catch the exception and then show the message to the client.

Another way is to check whether the user is in db with if condition and then respond back with message.. Instead of exception

which one is better and why? When to use If and when to catch an exception

Ramandeep S
  • 325
  • 1
  • 4
  • 15
  • 1
    Exceptions should be for unexpected behaviour, a user not being found is not an something out of the ordinary. – beresfordt Feb 25 '15 at 19:46

1 Answers1

1

This is based on the functionality.

  • consider the case where the user not in a db can be possible and a valid case then no need to throw exception.
  • consider the case where the user's data should be in db and its not found due to xyz reasons then throwing the exception makes sense.
anurag gupta
  • 379
  • 1
  • 5