zoukankan      html  css  js  c++  java
  • main 方法的书写(1)

    1、打印消息并且退出

    代码

    /**
    * <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、帮助信息的打印

    代码
    /**
    * <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.";
  • 相关阅读:
    修改MSSql数据库名
    系统更新0x8DDD0007号错误解决方案
    win7密匙 win7永久激活工具
    Ps制作的立体字效果
    PS合成人物与风景
    word打不开_如何删除normal.dot
    查看自己的IP地址和网卡的MAC地址
    char varchar nvarchar区别
    配置节点简单使用
    线程相关的概念
  • 原文地址:https://www.cnblogs.com/ITEagle/p/1774888.html
Copyright © 2011-2022 走看看