How to handle a static final field initializer that throws checked exception

后端 未结 4 1103
迷失自我
迷失自我 2020-12-02 17:01

I am facing a use case where I would like to declare a static finalfield with an initializer statement that is declared to throw a checked exception. Typically,

4条回答
  •  感动是毒
    2020-12-02 17:20

    static blocks aren't difficult to read. So I'd recommend that solution. However, you can wrap your object in another object, for example ObjectNameWrapper which shares an interface with your ObjectName, and whose constructor calls your ObjectName constructor, hiding all checked exceptions that occur. But again, I'd go for the static option.

提交回复
热议问题