Usage of try/catch blocks in C++

后端 未结 8 2130
有刺的猬
有刺的猬 2020-12-08 12:01

In general, I tend to use try/catch for code which has multiple failure points for which the failures have a common handler.

In my experience, this is typically co

8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 12:27

    In my experience the biggest problem with try/catch blocks are we often try to catch exceptions too generically. For instance, if I wrap my main function with a try/catch block that catches (...) I am basically trying to not allow my program to crash b/c of a thrown exception.

    The problem with this approach as I see it is two fold. 1) While I'm testing and debugging I don't see any errors and I don't get the opportunity to fix them. 2) It's really kind of taking the lazy way out. Instead of thinking through the problems that may come up and figuring out what the edge cases are I'm just trying not to fail. Trying not to fail is a lot different than trying to succeed.

提交回复
热议问题