user2430771
user2430771

Reputation: 1422

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. Under what conditions should actually throw a runtime exception? Should we actually throw runtime exception?

Thanks

Upvotes: 44

Views: 47361

Answers (3)

Ankur Singhal
Ankur Singhal

Reputation: 26077

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.

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 (e.g. NumberFormatException when parsing a malformed number string). 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. i.e.

throw new RuntimeException(originalException).

Upvotes: 5

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726987

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:

  • Passing invalid parameter values - This is the most common cause of runtime exceptions. Most parameter validation exceptions should be runtime exceptions. Java provides several subclasses to signal these specific problems.
  • Calling methods in a wrong sequence - This is another common cause. When certain methods cannot be called until a class finishes initialization or some other preparatory steps, calls at the wrong time should cause runtime exceptions.

In this sense, your code is fine: it fits squarely in the first category, i.e. 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.

Upvotes: 55

tckmn
tckmn

Reputation: 59343

IllegalArgumentException is, in fact, a subclass of 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.

Upvotes: 20

Related Questions