何时引发运行时异常?
最近,我面试了公司,他们给了我一个编码问题。我得到了与一副纸牌相关的程序,其中一种方法是洗牌。所以我把这个程序写成:
/** 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);
}
}
在上面的代码中,我抛出了我最怀疑的非法争论。在什么情况下应该实际引发运行时异常?我们是否应该实际引发运行时异常?
谢谢