How IllegalArgumentException automatically handled inside 'if' condition in java?

Java 8Object Oriented ProgrammingProgramming

Whenever you pass inappropriate arguments to a method or constructor, an IllegalArgumentException is thrown. It is a Runtime exception therefore there is no need to handle this at the time of compilation.

Example

The valueOf() method of the java.sql.Date class accepts a String representing a date in JDBC escape format yyyy-[m]m-[d]d and converts it into 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

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 date String in any other format this method throws 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

The setPriority() method of the Thread class accepts an integer value representing the priority of the thread and sets it to the current thread. But, the value passed to this method should be less than the maxpriority of the thread else, this method throws an IllegalArgumentException.

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 in if condition

While you use the methods that causes IllegalArgumentException, since you know the legal arguments of them, you can restrict/validate the arguments using if-condition before-hand and avoid the exception.

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

Enter the thread priority value:
15
Priority value should be less than: 10
raja
Published on 06-Aug-2019 14:37:09
Advertisements