Try/catch blocks inside constructors

前端 未结 1 542
伪装坚强ぢ
伪装坚强ぢ 2021-02-07 13:04

Is it a bad programming practice to have try/catch blocks inside constructors? Or does it make no difference as long as our programs handle typeinitializer exceptions gracefully

相关标签:
1条回答
  • 2021-02-07 13:31

    System.TypeInitializationException is thrown when a static constructor throws an exception, not on an instance constructor. Exceptions are thrown normally in instance constructors.

    That aside, there's nothing "wrong" with it any more than it is anywhere else; handle exceptions that you can properly recover from, and allow those that you can't to bubble up.

    0 讨论(0)
提交回复
热议问题