Can I use throws in constructor?

前端 未结 12 2126
迷失自我
迷失自我 2021-01-13 21:03

I have to initialize file objects inside the constructor and for handling the exception, is it efficient using throws or should I go for try/

12条回答
  •  逝去的感伤
    2021-01-13 21:35

    It's okay to throw an exception in the constructor. I know some of the Java library classes do so (URI for just one example). I think it's better to throw an exception than to return an object in an unknown or invalid state.

提交回复
热议问题