main 方法的书写(1)

1、打印消息并且退出

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   

/**
* <p>
* Prints out the given exception or message, and then exit.
* </p>
*
*
@param printUsage
* the flag indicates whether printing out the usage is needed.
*
@param exitCode
* the exit status.
*
@param exception
* the exception to print.
*
@param message
* the message to print.
*/
private static void printAndExit( boolean printUsage, int exitCode, Throwable exception, String message) {
if (message != null ) {
// Print out the error message
System.out.println(message);
}
if (exception != null ) {
// Print out the exception
exception.printStackTrace(System.out);
}



if (printUsage) {
// Print out the usage to the screen
System.out.println(USAGE_MESSAGE);
}

// Exit
System.exit(exitCode);
}

 

 

     注意上面参数中的exitCode,如果程序正常结束则为0,如果遇到错误,则根据错误码的含义以对应的错误码结束。

2、帮助信息的打印

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
/**
* <p>
* Represents the usage message.
* </p>
*/
private static final String USAGE_MESSAGE = " Usage: java -jar converter.jar "
+ " [-config=<configFile>] [-countryCode=<CODE1 CODE2 ... CODEN>] [-h|-help] " + LINE_SEP
+ " <configFile> Optional. The file path of the configuration file. "
+ " Default is 'config.properties'. If given, it must be the first parameter. " + LINE_SEP
+ " <CODE1 CODE2 ... CODEN> Optional. One or more country codes separated by white spaces "
+ " for which price files are to be converted. " + LINE_SEP
+ " -h|-help Optional. Prints out this usage message. " ;

 

 

转载于:https://www.cnblogs.com/ITEagle/archive/2010/07/10/1774888.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值