
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to solve an IllegalArgumentException in Java?
An IllegalArgumentException is thrown in order to indicate that a method has been passed an illegal argument. This exception extends the RuntimeException class and thus, belongs to those exceptions that can be thrown during the operation of the Java Virtual Machine (JVM). It is an unchecked exception and thus, it does not need to be declared in a method’s or a constructor’s throws clause.
Reasons for java.lang.IllegalArgumentException
- When Arguments out of range. For example, the percentage should lie between 1 to 100. If the user entered 101 then an IllegalArugmentExcpetion will be thrown.
- When argument format is invalid. For example, if our method requires date format like YYYY/MM/DD but if the user is passing YYYY-MM-DD. Then our method can’t understand then IllegalArugmentExcpetion will be thrown.
- When a method needs non-empty string as a parameter but the null string is passed.
Example1
public class Student { int m; public void setMarks(int marks) { if(marks < 0 || marks > 100) throw new IllegalArgumentException(Integer.toString(marks)); else m = marks; } public static void main(String[] args) { Student s1 = new Student(); s1.setMarks(45); System.out.println(s1.m); Student s2 = new Student(); s2.setMarks(101); System.out.println(s2.m); } }
Output
45 Exception in thread "main" java.lang.IllegalArgumentException: 101 at Student.setMarks(Student.java:5) at Student.main(Student.java:15)
Steps to solve IllegalArgumentException
- When an IllegalArgumentException is thrown, we must check the call stack in Java’s stack trace and locate the method that produced the wrong argument.
- The IllegalArgumentException is very useful and can be used to avoid situations where the application’s code would have to deal with unchecked input data.
- The main use of this IllegalArgumentException is for validating the inputs coming from other users.
- If we want to catch the IllegalArgumentException then we can use try-catch blocks. By doing like this we can handle some situations. Suppose in catch block if we put code to give another chance to the user to input again instead of stopping the execution especially in case of looping.
Example2
import java.util.Scanner; public class Student { public static void main(String[] args) { String cont = "y"; run(cont); } static void run(String cont) { Scanner scan = new Scanner(System.in); while( cont.equalsIgnoreCase("y")) { try { System.out.println("Enter an integer: "); int marks = scan.nextInt(); if (marks < 0 || marks > 100) throw new IllegalArgumentException("value must be non-negative and below 100"); System.out.println( marks); } catch(IllegalArgumentException i) { System.out.println("out of range encouneterd. Want to continue"); cont = scan.next(); if(cont.equalsIgnoreCase("Y")) run(cont); } } } }
Output
Enter an integer: 1 1 Enter an integer: 100 100 Enter an integer: 150 out of range encouneterd. Want to continue y Enter an integer:
- Related Articles
- How to handle IllegalArgumentException inside an if using Java
- How IllegalArgumentException automatically handled inside 'if' condition in java?
- When do IllegalStateException and IllegalArgumentException get thrown? in java?
- How to rethrow an exception in Java?\n
- How to generate an UnsupportedOperationException in Java?\n
- How many ways to synchronize an ArrayList in Java?\n
- How to use an ArrayList in lambda expression in Java?\n
- How to solve an Equation?
- How to use an enum with switch case in Java?\n
- How to handle an exception using lambda expression in Java?\n
- How to convert a String to an InputStream object in Java?\n
- How can we implement an editable JLabel in Java?\n
- How to create an immutable class with mutable object references in Java?\n
- How to solve diamond problem using default methods in Java
- How to convert an array to JSON Array using JSON-lib API in Java?\n

Advertisements