A simple test case to demonstrate my 2 problems:
public class Numbers {
private static void usage() {
System.err.println("Usage: java " + getClass().getName() + " range");
System.exit(1);
}
public static void main(String[] args) throws IOException {
try {
int range = Integer.parseInt(args[0]);
} catch (Exception e) {
usage();
}
}
}
- Can’t call
getClass()from a static method - If no arguments have been supplied at the command line, I’ll get
ArrayIndexOutOfBoundsExceptionmessage instead of theusage()output. Why doesn’t catch (Exception e) catch it?
Works for me, exception is caught.
Getting the class name from a static method without referencing the
Numbers.class.getName()is difficult.But I found this