IllegalArgumentException In Java
Chapter:
Exception Handling
Last Updated:
25-04-2016 15:19:53 UTC
Program:
/* ............... START ............... */
import java.io.File;
public class JavaIllegalArgumentException {
public static String createRelativePath(String parent, String filename) {
if (parent == null)
throw new IllegalArgumentException("path cannot be null!");
return parent + File.separator + filename;
}
public static void main(String[] args) {
// The following command will be successfully executed.
System.out.println(JavaIllegalArgumentException.createRelativePath("dir1", "file1"));
System.out.println();
// The following command throws an IllegalArgumentException.
System.out.println(JavaIllegalArgumentException.createRelativePath(null, "file1"));
}
}
/* ............... END ............... */
Output
Exception in thread "main" java.lang.IllegalArgumentException: The parent path cannot be null!
at ExceptionHandling.JavaIllegalArgumentException.createRelativePath(JavaIllegalArgumentException.java:10)
at ExceptionHandling.JavaIllegalArgumentException.main(JavaIllegalArgumentException.java:21)
Notes:
-
IllegalArgumentException indicates that a method is called with incorrect input arguments.
Tags
IllegalArgumentException, Java, Exception