23

If I were writing a Python function which received an argument with the correct type, but a value out of bounds, then I might throw a ValueError or one of its sub-classes.

For example, suppose I had a function which added up the prices on an invoice. If one of the prices had a negative value or one of the item-counts was not positive and non-zero I might raise VE.

My current project is in Java, but the situation is the same:

A pricing function accepts only numbers within a specific range - what's the most sensible kind of exception object to throw that tells another Java developer their input is out of that sensible range?

3 Answers 3

25

The standard library defines IllegalArgumentException:

throw new IllegalArgumentException(); 
Sign up to request clarification or add additional context in comments.

1 Comment

No, it's called IllegalArgumentException, not InvalidArgumentException.
5

I would use an IllegalArgumentException for this purpose.

Comments

3

java.lang.IllegalArgumentException.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.