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


There are Java for many years, so C ++ has not been tracking what's the C ++ exception in the language definition The finally section has been added to the handling?

Is there any favored idiom that try / java in the end?

I can also write:

I can also write:

I can also write:

Code > Try {// do something} catch (...) {// Regrettably, can not check exceptions / / can only clean code and maybe restored, ala: throw; }

Edit adidum:

I had stopped accepting the reply which was the most votes, i.e. Clean the use of destructors, of course, with my own comments, it is clear that I am not fully convinced. However, C ++ is what it is like and keeping in mind this kind of application effort, I am trying more or less to follow the common community practice. I will use template classes which do not already have a class destructor (i.e. C library resource), thus giving them district semantics.

Edit new adendame:

Hmm, finally Instead of a closed facility? Combined with the ScopeGuard approach, a closer (see one of the answers below) will be a way to accomplish cleaning with arbitrary actions and access to the context of the outer scope of the cleanup code. Ruby programming can be cleaned in the style of idiom which can be seen in Ruby programming where pullup blocks are supplied when a resource is opened. What is the stop facility being considered for C ++?

By effectively using the destroyer. When an attempt is thrown into the block, any object created in it will be destroyed immediately (and therefore its destructor is called).

This is different from Java, where no object will be in the final form

UPDATE : directly from the mouth of the horse:


Comments