当“给定ID的用户不存在”时,我应该抛出IllegalArgmentException吗? [英] Should I throw IllegalArgmentException when 'user of given id not exist'?

查看:139
本文介绍了当“给定ID的用户不存在”时,我应该抛出IllegalArgmentException吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我们有一个方法 changeUserName(Long id,String newName),它调用存储库的 findUser(Long id)查找正确的用户实体,然后更改其名称。
findUser 返回null时,抛出 IllegalArgmentException 是否合适?
还是我应该抛出自定义 UserNotExistException (扩展AppException扩展RuntimeException)?

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



更新:


  • RuntimeException

    @nachokk @JunedAhsan实际上,我故意将所有异常取消选中,因为我认为这样可以使客户端代码干净,易于调试且更安全。至于那些未处理的,我会把它们全部捕获在顶层上,从而避免在UI上显示它们。

    这是由于许多客户捕获了已检查的异常,然后将其忽略,在某些情况下,他们不知道如何处理。这是一个隐藏的麻烦。

  • RuntimeException:
    @nachokk @JunedAhsan Actually I deliberately make all the exceptions unchecked , 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 catch checked exceptions and then just ignore it, and in some cases they don't know how to handle it. This is a hidden trouble.

说明:

对不起,我的英语不好。我的意思是,如果 changeUserName 应该抛出 IllegalArgumentException ,而不是 findUser 方法。还有另一个问题:如何区分非法论点违反业务规则

Clarification:
Sorry for my bad English. What I meant is if the changeUserName should throw an IllegalArgumentException, not the findUser method. And another question: how to differentiate illegal argument from business rule violation?

推荐答案

您应使用 UserNotExistException 。名称非常说明正在发生的事情。我认为您应该避免返回 null ,但是如果您这样做,则必须对其进行记录。

You should use UserNotExistException. The name is very declarative of what is happening. In my opinion you should to avoid returning null but if you do you have to document it.

UPDATE

我在想,正如@JunedAhsan建议的那样, UserNotExistException 可能会更好。 $ c> CheckedException (从Exception而不是RuntimeException扩展)。

I was thinking and as @JunedAhsan suggest, UserNotExistException could be better a CheckedException (extends from Exception and not RuntimeException).

通过此链接:未经检查的异常:争议


如果可以合理预期客户会从异常中恢复,则
使其成为受检查的异常。如果客户无法采取任何措施从异常中恢复
,请将其设为未经检查的异常。

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.



/**
* @return User found or throw UserNotExistException if is not found
*/
public User findUser(Long id) throws UserNotExistException{
    //some code
    User user = giveMeUserForSomePlace();
    if(user == null){
        throw new UserNotExistException();
    }  
    return user;   
}

这篇关于当“给定ID的用户不存在”时,我应该抛出IllegalArgmentException吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆