Is there an appropriate exception class for invalid command line arguments in the Java API or do I have to create my own? I've tried searching for one but can't find any in the API.
This is for an assignment so I cannot use third party libraries for command line parsing.
We can check these arguments using args. length method. JVM stores the first command-line argument at args[0], the second at args[1], the third at args[2], and so on.
A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched. When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of String s.
Java command line arguments are arguments that are passed to your program via a terminal or shell command. They make your Java program more configurable by giving users or scripts running your software a way to specify input parameters at run time.
Most of the times, when the received argument(s) are invalid, it's a common idiom to throw an IllegalArgumentException
.
public class IllegalArgumentException extends RuntimeException
Thrown to indicate that a method has been passed an illegal or inappropriate argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With