Java Invalid Command Line Arguments Exception
Solution 1
The best way to handle unknown command line parameters or combinations that don't make sense to the program is to display an error message and offer a usages output.
Personally, depending on the complexity of the command line, I will create a method called "usage" (usually static) that can have an optional error message passed to it.
While parsing the command line parameters passed into the program, I will call this method and either exit, via a flag or directly, or have usage
method call exit for me.
But that's just me
Solution 2
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.
![Michael](https://i.stack.imgur.com/7Ekpp.jpg?s=256&g=1)
Michael
Updated on July 19, 2022Comments
-
Michael almost 2 years
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.
-
Michael almost 12 yearsI did consider using this, but I thought that RuntimeExceptions (unchecked exceptions) are supposed to reflect a problem that the programmer has done wrong whereas checked exceptions would be used for things outside the programmer's control. Or does this only matter when designing API's?
-
João Silva almost 12 yearsI tend to use unchecked exceptions for situations where your program cannot recover at runtime. An invalid argument passed to a method is such a case, it's a programmer's error. For example, when you try to instantiate an
ArrayList
with a negative initial capacity, it will throw anIllegalArgumentException
. Why? Because it's not supposed to receive that, and it can't work with a negative capacity; it's a programmer's error. Here's a link that discusses this issue javapractices.com/topic/TopicAction.do?Id=129. -
João Silva almost 12 yearsAlso, if you use a checked exception, every method that calls yours needs to catch this exception. Imho, this is somewhat odd, because basically you are acknowledging that you can have errors in your code that makes it pass invalid values/arguments to another method. And when this happens, there's nothing you can do about it, but fix the error that caused this.
-
Michael almost 12 yearsWhen a program is given invalid command line arguments it is not a programmer error though, its a user error.
-
João Silva almost 12 years@Michael: I agree, but those invalid arguments should never reach a method that doesn't support them, they should be pre-validated. If they do reach, then an
IllegalArgumentException
can be thrown. Now, if you want to terminate the program and show the user that he has passed invalid arguments, then you can just write toSystem.err
telling why the arguments are invalid. This is whatjavac
does, for example. If you invoke it with, say,javac -abcd
, it will terminate with the messagejavac: invalid flag: -abcd
; no reason to throw an Exception in this case, imho. -
Michael almost 12 yearsYeah, I ended up not throwing an Exception as I was just catching it in the same method anyway.