Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java what is idiomatically most similar to raising a Python ValueError?

Tags:

java

python

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?

like image 590
Salim Fadhley Avatar asked Feb 09 '12 15:02

Salim Fadhley


3 Answers

The standard library defines IllegalArgumentException:

throw new IllegalArgumentException();
like image 189
liwp Avatar answered Nov 08 '22 12:11

liwp


I would use an IllegalArgumentException for this purpose.

like image 27
Jesper Avatar answered Nov 08 '22 11:11

Jesper


java.lang.IllegalArgumentException.

like image 3
ruakh Avatar answered Nov 08 '22 12:11

ruakh