简体   繁体   English

在这种情况下抛出什么适当的异常?

[英]What's the appropriate exception to throw in this case?

public Item getItem(String name){
    if(someContainer.containsKey(name)) return someContainer.get(name);
    throw new /*...*/Exception("Item doesn't exist");
}

What kind of exception is appropriate to throw in this case? 在这种情况下,哪种异常合适? IllegalArgumentException doesn't feel quite right. IllegalArgumentException感觉不太正确。

Just create your own (how about ItemNotFoundException ?) 只需创建自己的( ItemNotFoundException怎么ItemNotFoundException ?)

It makes catching and reacting to it easier, too, because you can catch exactly that exception, and not an IllegalArgumentException that might be thrown somewhere else. 它也使捕获和响应它变得更加容易,因为您可以捕获到该异常,而不是可能抛出其他地方的IllegalArgumentException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM