[英]When to throw runtime exception?
Recently, I had interview with company and they gave me a coding problem. 最近,我接受了公司的采访,他们给了我一个编码问题。 I was given program related to deck of cards and one of the methods was to shuffle the deck of cards.
我得到了与卡片组相关的程序,其中一种方法是洗牌。 So I wrote the program as:
所以我把程序写成:
/** Shuffle the list of cards so that they are in random order
* @param d Deck of cards*/
public static void shuffle(Deck d)
{
if(d == null)
throw new IllegalArgumentException();
Random randomGenerator = new Random();
List<Card> cards = d.getDeckOfCards(); // cards is basically Linked List.. cards = new LinkedList<Cards>()
for(int i=0;i<cards.size();i++)
{
int randomNumber = randomGenerator.nextInt(52);
Card c1 = cards.remove(randomNumber);
Card c2 = cards.remove(0);
cards.add(0, c1);
cards.add(randomNumber,c2);
}
}
In the above code, I have thrown IllegalArgumentException which I'm most doubtful about. 在上面的代码中,我抛出了IllegalArgumentException ,这是我最怀疑的。 Under what conditions should actually throw a runtime exception?
在什么条件下应该实际抛出运行时异常? Should we actually throw runtime exception?
我们应该抛出运行时异常吗?
Thanks 谢谢
Should we actually throw runtime exception?
我们应该抛出运行时异常吗?
Yes, we should. 是的,我们应该。 Runtime exception serve a specific purpose - they signal programming problems that can be fixed only by changing code, as opposed to changing the environment in which the program runs.
运行时异常用于特定目的 - 它们表示编程问题,只能通过更改代码来修复,而不是更改程序运行的环境。
Under what conditions should actually throw a runtime exception?
在什么条件下应该实际抛出运行时异常?
When you detect an error with the way your class or method is used, throw a runtime exception. 使用类或方法的方式检测错误时,抛出运行时异常。
Generally, there are two categories of situations when you need to throw a runtime exception: 通常,当您需要抛出运行时异常时,有两种情况:
In this sense, your code is fine: it fits squarely in the first category, ie passing invalid parameter values. 从这个意义上说,你的代码很好:它完全适合第一类,即传递无效的参数值。 One thing I would do slightly differently is adding a message to say which parameter had an invalid value, but in your case it is not critical, because there is only one parameter there.
我会做的一件事略有不同的是添加一条消息来说明哪个参数的值无效,但在你的情况下它并不重要,因为那里只有一个参数。
IllegalArgumentException
is, in fact, a subclass of RuntimeException
. 实际上,
IllegalArgumentException
是RuntimeException
的子类 。
It's just better to be a bit more specific, to help other programmers in the future. 最好是更具体一点,以帮助其他程序员。 I would definitely prefer the
IllegalArgumentException
because it best describes what exactly went wrong, but really, either one would work. 我肯定更喜欢
IllegalArgumentException
因为它最好地描述了究竟出了什么问题,但实际上,任何一个都可以工作。
For example, if you're reading a file , and some IO error occurs, you're unlikely to recover from the error, so re-throwing the error to the top and consequently terminating the application isn't a bad course of action. 例如,如果您正在读取文件,并且发生了一些IO错误,则您不太可能从错误中恢复,因此将错误重新抛到顶部并因此终止应用程序并不是一个糟糕的行动方案。
On the other hand, if you're anticipating recoverable errors then you should absolutely catch and handle the errors. 另一方面,如果您预计可恢复的错误,那么您应该绝对捕获并处理错误。 For example, you may have users entering data in a form.
例如,您可能让用户在表单中输入数据。 If they enter data incorrectly, your input processing code may throw an exception (eg
NumberFormatException
when parsing a malformed number string). 如果输入数据不正确,输入处理代码可能会抛出异常(例如,在解析格式错误的数字字符串时会出现
NumberFormatException
)。 Your code should catch these exceptions and return an error the user, prompting for correct input. 您的代码应捕获这些异常并向用户返回错误,提示输入正确。
When catching an exception and throwing RuntimeException
instead, it is important to set the original exception as a cause for the RuntimeException . 捕获异常并抛出
RuntimeException
,将原始异常设置为RuntimeException的原因非常重要。 ie 即
throw new RuntimeException(originalException)
. 抛出新的
RuntimeException(originalException)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.