Class
Java IllegalArgumentException
Unchecked exception for an inappropriate argument.
Formal Syntax & Structure
java syntaxjava
new IllegalArgumentException(String message)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| message | String | Detail message |
Working Example
Example Snippetjava
public class Main { static int quantity(int n) { if (n < 0) throw new IllegalArgumentException("Negative quantity"); return n; } public static void main(String[] args) { try { quantity(-2); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } System.out.println(quantity(3)); }}