Is there a favored idiom for mimicing Java's try/finally in C++?

前端 未结 15 803
无人及你
无人及你 2021-02-05 19:16

Been doing Java for number of years so haven\'t been tracking C++. Has finally clause been added to C++ exception handling in the language definition?<

15条回答
  •  花落未央
    2021-02-05 19:47

    C++'s answer is RAII: The object's destructor will be executed when they go out of scope. Whether by a return, by an exception or whatever. If you handle the exception somewhere else, you can be sure all objects from the called function down to your handler will be properly destructed by having their destructor called. They will clean up for you.

    Read http://en.wikipedia.org/wiki/Resource_acquisition_is_initialization

提交回复
热议问题