Exception handling before and after main

后端 未结 4 754
再見小時候
再見小時候 2021-02-15 17:52

Is it possible to handle exceptions in these scenarios:

  1. thrown from constructor before entering main()
  2. thrown from destructor after leaving main()
4条回答
  •  遥遥无期
    2021-02-15 18:11

    It might be possible to set an exception handler before construction / destruction of the objects in question, that one should be able to handle those exceptions.

    For constructors, there's some weird new syntax that allows exceptions to be caught within the constructor. Not sure how that works, and it's not commonly implemented in many compilers.

    For destructors, you have to wrap the content of the destructor in a try { code(); } catch(...) {} block. Which may not always be the desired behavior, depending on what you want to achieve in that destructor.

提交回复
热议问题