【问题标题】:How to throw custom exception with empty list instead of null?如何用空列表而不是null引发自定义异常?
【发布时间】:2020-02-28 13:49:27
【问题描述】:

我在 Test1 类中抛出自定义异常并将列表传递给构造函数:

    public class Test1 {
        public List<Journal> method1(String str) throws SectionNotFoundException {
        List<Journal> list = new ArrayList<>();
        //...
        else if(...) {
            throw new SectionNotFoundException(list);
        }
        //...
    }
}

它让我回到我在 Test1 类内调用 method1 的行 Test2 就像:

   public class Test2 {
     //...     
     public void method() {
     //...
     Test1 test1 = new Test1();
     try {
         list = test1.method1(text); 
         } catch (SectionNotFoundException e) {
         //...
         }
     }
 }

在名为SectionNotFoundException 的自定义异常中,我想获取空列表而不是null

public class SectionNotFoundException extends Throwable {
    List<Journal> journalList;

    public SectionNotFoundException(List<Journal> journalList) {
        this.journalList = journalList;
        emptyArrayList();
    }

    public ArrayList<Object> emptyArrayList() {
        return new ArrayList<>();
    }
}

但真正的问题是,如果emptyList的返回值从未被使用过,如何正确写入。

【问题讨论】:

  • 使用异常来抽象一些值并不是一个好习惯,比如这里的List&lt;Journal&gt; journalList。您可以在捕获异常的地方分配一个空列表。
  • @invzbl3 通过将一个空列表传递给您的构造函数?
  • @Stultuske 如果我的列表返回null,我会抛出异常,但我正在尝试在自定义异常类中更改它,如您所见我的问题。跨度>
  • @inzvbl,不,你会一直覆盖它。尝试(在您的构造函数中)类似: this.journalList = journalList == null ?新的 ArrayList() : journalList;
  • 现在说得通了。感谢您指出,我会测试并纠正它。

标签: java exception arraylist


【解决方案1】:

按如下方式进行:

public class SectionNotFoundException extends Throwable {
    List<Journal> journalList;

    public SectionNotFoundException(List<Journal> journalList) {
        if(journalList == null) {
            this.journalList = new ArrayList<>();
        } else {
            this.journalList = journalList;
        }
    }
}

速记(根据 Stultuske 的建议):

public class SectionNotFoundException extends Throwable {
    List<Journal> journalList;

    public SectionNotFoundException(List<Journal> journalList) {
         this.journalList = journalList == null ? new ArrayList<>() : journalList; 
    }
}

【讨论】:

  • 为什么不用简单的三元运算符?
  • if...else 有什么问题?
  • 可读性。和干净的代码。特别是对于如此简单的事情,三元运算符提供了一个很好且简单的解决方案,为您节省了大约四行代码。
【解决方案2】:

自己纠正一下:

我首先抛出自定义异常,而没有将任何东西传递给构造函数(因为我之前做错了),例如:

throw new SectionNotFoundException();

同时,根据我的要求,我可以使用返回空列表的方法编写自定义异常:

public class SectionNotFoundException extends Throwable {    
    public ArrayList<...> emptyArrayList() {
        return new ArrayList<>();
    }
}

然后我只是忘记在某个地方调用它来使用这个方法,所以当我完全捕捉到这个异常时我就这样做了:

   public class Test2 {
     //...     
     public void method() {
     //...
     Test1 test1 = new Test1();
         try {
            list = test1.method1(text); // generating SectionNotFoundException
         } catch (SectionNotFoundException e) {
            list = e.emptyArrayList(); // assign empty list instead of null by calling emptyArrayList method inside class of custom exception
         }
     }
 }

附:我知道它并不能完全回答我的问题,而是我当时想要实现的目标。

【讨论】:

    猜你喜欢
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    相关资源
    最近更新 更多