当我浏览一些教程时,其中一个源代码文件有以下内容检查是否没有命令行参数:
if (null==args[0]) {
System.err.println("Properties file not specified at command line");
return;
}
由于显而易见的原因抛出ArrayIndexOutOfBoundsException并且不打印消息。
那么,如何检查并打印消息而不会抛出异常?
答案 0 :(得分:7)
if (args.length == 0) {
System.err.println("Properties file not specified at command line");
return;
}
如果命令行中没有参数,则参数数组将为空。因此,您需要检查其长度args.length==0
。
答案 1 :(得分:1)
if (args.length == 0)
检查长度。