Usage of try/catch blocks in C++

后端 未结 8 2126
有刺的猬
有刺的猬 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条回答
  •  情话喂你
    2020-12-08 12:16

    In C++ you shouldn't use try/catch blocks to perform cleanup. Instead, you might want to use templates to perform resource aquisition.

    auto_ptr's are one [bad] example

    Synchronization locks, where you store a mutex as a state variable, and use local variable (templates or regular classes) to perform the .acquire()/.release() methods.

    The more of this you do, the less you have to worry about manually releasing objects in exceptional conditions. The C++ compiler will do it for you.

提交回复
热议问题