- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How IllegalArgumentException automatically handled inside 'if' condition in java?
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
- Related Articles
- How to handle IllegalArgumentException inside an if using Java
- How to solve an IllegalArgumentException in Java?\n
- What happens if an exception is not handled in a java program?
- What happens if NullPointerException is not handled in main method of java?
- When do IllegalStateException and IllegalArgumentException get thrown? in java?
- Apply a condition inside subset in MongoDB Aggregation?
- How do we close resources automatically in Java?
- How to automatically resize a JTree in Java
- How To Check if a Given Point Lies Inside a Rectangle in Java?
- How to automatically close all collapsible elements inside of the accordion when closing the accordion?
- Why is iostream::eof inside a loop condition considered wrong?
- How to use if/else condition in select in MySQL?
- How to Automatically Create Borders if a Cell has Contents in Excel?
- How to use OR condition in a JavaScript IF statement?
- How can I add condition in custom exceptions in java?
