2013-03-04 44 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) 

只要檢查長度。

相關問題