2013-03-04 75 views
1

当我经历了一些教程中的源代码文件的一人跟随检查,如果没有命令行参数:检查是否有关于命令没有参数

if (null==args[0]) { 
    System.err.println("Properties file not specified at command line"); 
    return; 
} 

其中的原因很明显抛出ArrayIndexOutOfBoundsException异常并不打印该消息。

那么,如何做到这一点检查并打印消息而不会引发异常?

回答

7
if (args.length == 0) { 
    System.err.println("Properties file not specified at command line"); 
    return; 
} 

当命令行中没有参数时,参数数组将为空。所以,你检查它的长度args.length==0

1
if (args.length == 0) 

只要检查长度。

相关问题