English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to automatically handle IllegalArgumentException within the 'if' condition in Java?

IllegalArgumentException will be thrown whenever you pass inappropriate parameters to a method or constructor. This is a runtime exception, so there is no need to handle this exception at compile time.

Example

valueOf()The methods of the java.sql.Date class accept a date in JDBC escape formatyyyy- [m] m- [d] dRepresent the date as a String and convert it to a java.sql.Date object.

import java.sql.Date;
import java.util.Scanner;
public class IllegalArgumentExample {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your date of birth in JDBC escape format (yyyy-mm-dd) ");
      String dateString = sc.next();
      Date date = Date.valueOf(dateString);
      System.out.println("Given date converted int to an object: "+date);
   }
}

Output Result

Enter your date of birth in JDBC escape format (yyyy-mm-dd)
1989-09-26
Given date converted into an object: 1989-09-26

But if you pass the date String in any other format, this method will throw an IllegalArgumentException.

import java.sql.Date;
import java.util.Scanner;
public class IllegalArgumentExample {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter your date of birth in JDBC escape format (yyyy-mm-dd) ");
      String dateString = sc.next();
      Date date = Date.valueOf(dateString);
      System.out.println("Given date converted int to an object: "+date);
   }
}

Runtime exception

Enter your date of birth in JDBC escape format (yyyy-mm-dd)
26-07-1989
Exception in thread "main" java.lang.IllegalArgumentException
   at java.sql.Date.valueOf(Unknown Source)
   at july_ipoindi.NextElementExample.main(NextElementExample.java:11)
In the following Java example, the Date constructor (actually deprecated) accepts

Example

setPriority()The methods of the Thread class accept an integer value representing the thread priority and set it to the current thread. However, the value passed to this method should be less than the maximum priority of the thread; otherwise, this method will throwIllegalArgumentException.

public class IllegalArgumentExample {
   public static void main(String args[]) {
      Thread thread = new Thread();
      System.out.println(thread.MAX_PRIORITY);
      thread.setPriority(12);
   }
}

Runtime exception

10Exception in thread "main"
java.lang.IllegalArgumentException
   at java.lang.Thread.setPriority(Unknown Source)
   at july_ipoindi.NextElementExample.main(NextElementExample.java:6)

Handling IllegalArgumentException under the if condition

When using methods that cause IllegalArgumentException, since you know their valid parameters, you can use if in advance-condition limitation/Validate parameters and avoid exceptions.

Example

import java.util.Scanner;
public class IllegalArgumentExample {
   public static void main(String args[]) {
      Thread thread = new Thread();
      System.out.println("Enter the thread priority value: ");
      Scanner sc = new Scanner(System.in);
      int priority = sc.nextInt();
      if(priority<=Thread.MAX_PRIORITY) {
         thread.setPriority(priority);
      }else{
         System.out.println("Priority value should be less than: ");+Thread.MAX_PRIORITY);
      }
   }
}

Output Result

Enter the thread priority value:
15
Priority value should be less than: 10