Say we have a method changeUserName(Long id,String newName)
which invokes the repository's findUser(Long id)
to find the right user entity and then change its name.
Is it appropriate to thow an IllegalArgmentException
when findUser
returns null ?
Or should I instead throw a custom UserNotExistException
(extends AppException extends RuntimeException) ?
UPDATE:
RuntimeException
:
@nachokk @JunedAhsan Actually I deliberately make all the exceptionsunchecked
, because I think this way makes client code clean , easy for debuging and more safe. As to those "unhandled" ones, I'll catch them all on the top of layers thus avoiding showing them on the UI.
This is due to the fact that many clients catchchecked exceptions
and then just ignore it, and in some cases they don't know how to handle it. This is a hidden trouble.Clarification:
Sorry for my bad English. What I meant is if the changeUserName should throw anIllegalArgumentException
, not thefindUser
method. And another question: how to differentiateillegal argument
frombusiness rule violation
?