有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

创建自己的Java异常子类

我正在为我创建的一个简单命令行工具创建自己的异常类。基本上,目的是决定何时抛出致命异常,并在这种情况下终止程序。所以我的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;
    }

}

共 (2) 个答案

  1. # 1 楼答案

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

    这将要求您修改方法签名,以包括throws ...,向调用者指示您的方法可能引发的异常:

    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.");
         }
            ...
    
  2. # 2 楼答案

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

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