Can I use throws in constructor?

前端 未结 12 2123
迷失自我
迷失自我 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:20

    You sure can (for example, FileOutputStream does).

    Throwing an exception from the constructor should be done wisely - make sure you clean up after yourself cleanly. Throwing exceptions in a constructor is sometimes done in order to make sure RAII is held.

提交回复
热议问题