Java args4j:在解析过程中,哪个参数引发了CommandLineException?

Java args4j:在解析过程中,哪个参数引发了CommandLineException?,java,Java,我正在使用args4j解析给定给程序的参数 下面是我定义2个日期类型参数的代码。处理程序只解析给定的日期,如果日期格式不正确,则抛出CommandLineException @Option(name="-b", metaVar="<beginDate>", handler=DateOptionHandler.class, usage="...") private Date beginDate; @Option(name="-e", metaVar="<endDate>"

我正在使用args4j解析给定给程序的参数

下面是我定义2个日期类型参数的代码。处理程序只解析给定的日期,如果日期格式不正确,则抛出CommandLineException

@Option(name="-b", metaVar="<beginDate>", handler=DateOptionHandler.class, usage="...")
private Date beginDate;

@Option(name="-e", metaVar="<endDate>", handler=DateOptionHandler.class, usage="...")
private Date endDate;
但我不知道如何知道哪个参数引发了异常(我查看了CmdLineException方法,但什么也没找到)

是否有方法获取无法解析的参数


感谢advance提供的帮助。

我从未使用过args4j,但查看其文档,异常似乎是由选项处理程序引发的。因此,请使用BDateOptionHandler和EDateOptionHandler,它们抛出包含所需信息的CmdLineException自定义子类:

public class BDateOptionHandler extends DateOptionHandler {
    @Override
    public int parseArguments(Parameters params) throws CmdLineException {
        try {
            super.parseArguments(params);
        }
        catch (CmdLineException e) {
            throw new ErrorCodeCmdLineException(2);
        }
    }
}

public class EDateOptionHandler extends DateOptionHandler {
    @Override
    public int parseArguments(Parameters params) throws CmdLineException {
        try {
            super.parseArguments(params);
        }
        catch (CmdLineException e) {
            throw new ErrorCodeCmdLineException(3);
        }
    }
}

...
try {
    parser.parseArgument(args);
} 
catch (CmdLineException e) {
    ...
    if (e instanceof ErrorCodeCmdLineException) {
        return ((ErrorCodeCmdLineException) e).getErrorCode();
    }
}

你不能通过检查startDate==null或endDate==null来判断它吗?谢谢你的回答。不幸的是,beginDate和endDate是可选的,因此如果省略beginDate并且endDate抛出异常,它将返回错误的错误代码。
public class BDateOptionHandler extends DateOptionHandler {
    @Override
    public int parseArguments(Parameters params) throws CmdLineException {
        try {
            super.parseArguments(params);
        }
        catch (CmdLineException e) {
            throw new ErrorCodeCmdLineException(2);
        }
    }
}

public class EDateOptionHandler extends DateOptionHandler {
    @Override
    public int parseArguments(Parameters params) throws CmdLineException {
        try {
            super.parseArguments(params);
        }
        catch (CmdLineException e) {
            throw new ErrorCodeCmdLineException(3);
        }
    }
}

...
try {
    parser.parseArgument(args);
} 
catch (CmdLineException e) {
    ...
    if (e instanceof ErrorCodeCmdLineException) {
        return ((ErrorCodeCmdLineException) e).getErrorCode();
    }
}