
- 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 handle IllegalArgumentException inside an if using Java
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.
We can restrict the argument value of a method using the if statement. For example, if a method accepts values of certain range, you can verify the range of the argument using the if statement before executing the method.
Example
Following example handles the IllegalArgumentException caused by the setPriority() method using the if statement.
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 IllegalArgumentException automatically handled inside 'if' condition in java?
- How to solve an IllegalArgumentException in Java?\n
- How to handle python exception inside if statement?
- How to handle an exception using lambda expression in Java?\n
- How to handle bind an event using JavaScript?
- How to handle exception inside a Python for loop?
- How to handle frame in Selenium WebDriver using java?
- How to handle the exception using UncaughtExceptionHandler in Java?
- How to handle authentication popup with Selenium WebDriver using Java?
- How to handle an exception in JShell in Java 9?
- How to change text inside an element using jQuery?
- How to handle EOFException in java?
- How to handle MalformedURLException in java?
- How to handle StringIndexOutOfBoundsException in Java?
- How to write a class inside an interface in Java?

Advertisements