创建自己的Java异常子类

创建自己的Java异常子类,java,exception,Java,Exception,我正在为我创建的一个简单命令行工具创建自己的异常类。基本上,目的是决定何时抛出致命异常,并在这种情况下终止程序。所以我的ReconToolException类有一个布尔标志来记录致命异常 然而,我的问题是,“抛出”异常的正确方式是什么 我应该这样做吗 if(columnNumberForGroupByColumnFile1 == null || columnNumberForGroupByColumnFile2 == null || columnNumberForCoun

我正在为我创建的一个简单命令行工具创建自己的异常类。基本上,目的是决定何时抛出致命异常,并在这种情况下终止程序。所以我的ReconToolException类有一个布尔标志来记录致命异常

然而,我的问题是,“抛出”异常的正确方式是什么

我应该这样做吗

if(columnNumberForGroupByColumnFile1 == null || columnNumberForGroupByColumnFile2 == null ||
            columnNumberForCountColumnFile1 == null || columnNumberForCountColumnFile2 == null ){

              new ReconToolException("Can't find column number. Likely that there are unexpected headers in the CSV files. Check your CSV configuration enum .java files to see if they match the actual input CSV files.");

    }
或者我应该用下面的方法来做

if(columnNumberForGroupByColumnFile1 == null || columnNumberForGroupByColumnFile2 == null ||
                columnNumberForCountColumnFile1 == null || columnNumberForCountColumnFile2 == null ){
             try {
                 throw new ReconToolException("Can't find column number. Likely that there are unexpected headers in the CSV files. Check your CSV configuration enum .java files to see if they match the actual input CSV files.");
            } catch (ReconToolException e) {
                e.printStackTrace();
            }
        }
前者似乎要求我在ReconToolException类中显式地编写一个打印输出语句,以便将错误打印到命令行。后者将捕获自身(这非常奇怪,而且看起来确实不正确),并将在catch块中打印堆栈跟踪。前者似乎更有意义,但后者似乎效果更好。有正确的方法吗

以下是我创建的异常类:

public class ReconToolException extends Exception {

    private boolean isFatal;

    public ReconToolException() {
        // TODO Auto-generated constructor stub
    }
    public ReconToolException(boolean isFatalException, String msg) {
        super(msg);
        this.setFatal(isFatalException);
    }

    public ReconToolException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public ReconToolException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }

    public ReconToolException(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }

    public ReconToolException(String message, Throwable cause,
            boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        // TODO Auto-generated constructor stub
    }


    public boolean isFatal() {
        return isFatal;
    }

    public void setFatal(boolean isFatal) {
        this.isFatal = isFatal;
    }

}

您应该简单地
抛出
异常,而不是自己在抛出异常的方法中捕获它,除非您想在那里捕获它。通常情况下,如果您自己创建异常,则不会。但是,此方法的其他调用方可能希望捕获它

这将要求您修改方法签名以包括
抛出…
向调用方指示您的方法可能抛出的异常:

if(columnNumberForGroupByColumnFile1 == null || columnNumberForGroupByColumnFile2 == null ||
                columnNumberForCountColumnFile1 == null || columnNumberForCountColumnFile2 == null ){

        throw new ReconToolException("Can't find column number. Likely that there are unexpected headers in the CSV files. Check your CSV configuration enum .java files to see if they match the actual input CSV files.");
     }
        ...

如果采用第二种方法(即捕获异常),则
isFatal
标志没有任何意义。如果您只是简单地抛出并立即捕获异常,那么您只是显示了一个错误堆栈跟踪,并且不允许异常向上传播方法调用序列,因此它无法在致命的情况下终止程序


您应该抛出异常,并让方法的调用方来处理它。

您为什么认为需要显式打印消息?通常,如果您正在运行控制台应用程序,并且它通过异常终止,则会自动打印异常。我强烈建议您考虑将致命的和非致命的异常分为两种类型:致命的异常可能是子类<代码> RunTimeExtExabor >代码>。我只是把它作为结束他<代码>的指示,如果块,如果条件不满足,继续执行。答案已更新。
自己不捕获它
:在
if
本身中,它是没有意义的,但是在调用此方法的任何方法中捕获它,或者在调用此方法的任何方法中捕获它,以管理那里的异常,都可能是有用的。应根据需要对其进行管理。