Quantcast
Channel: Does C++ support 'finally' blocks? (And what's this 'RAII' I keep hearing about?) - Stack Overflow
Browsing all 17 articles
Browse latest View live

Answer by Dean Roddey for Does C++ support 'finally' blocks? (And what's this...

I also think that RIIA is not a fully useful replacement for exception handling and having a finally. BTW, I also think RIIA is a bad name all around. I call these types of classes 'janitors' and use...

View Article



Answer by tobi_s for Does C++ support 'finally' blocks? (And what's this...

As pointed out in the other answers, C++ can support finally-like functionality. The implementation of this functionality that is probably closest to being part of the standard language is the one...

View Article

Answer by anton_rh for Does C++ support 'finally' blocks? (And what's this...

Another "finally" block emulation using C++11 lambda functionstemplate <typename TCode, typename TFinallyCode>inline void with_finally(const TCode &code, const TFinallyCode...

View Article

Answer by Fabio A. for Does C++ support 'finally' blocks? (And what's this...

I came up with a finally macro that can be used almost like¹ the finally keyword in Java; it makes use of std::exception_ptr and friends, lambda functions and std::promise, so it requires C++11 or...

View Article

Answer by Mark Lakata for Does C++ support 'finally' blocks? (And what's this...

I have a use case where I think finallyshould be a perfectly acceptable part of the C++11 language, as I think it is easier to read from a flow point of view. My use case is a consumer/producer chain...

View Article


Answer by jave.web for Does C++ support 'finally' blocks? (And what's this...

EDITEDIf you are not breaking/continuing/returning etc., you could just add a catch to any unknown exception and put the always code behind it. That is also when you don't need the exception to be...

View Article

Answer by Paolo.Bolzoni for Does C++ support 'finally' blocks? (And what's...

RAII is usually better, but you can have easily the finally semantics in C++. Using a tiny amount of code.Besides, the C++Core Guidelines give finally.Here is a link to the GSL Microsoft implementation...

View Article

Answer by Mark Lakata for Does C++ support 'finally' blocks? (And what's this...

As many people have stated, the solution is to use C++11 features to avoid finally blocks. One of the features is unique_ptr.Here is Mephane's answer written using RAII patterns.#include...

View Article


Answer by bcmpinc for Does C++ support 'finally' blocks? (And what's this...

Not really, but you can emulate them to some extend, for example:int * array = new int[10000000];try { // Some code that can throw exceptions // ... throw std::exception(); // ...} catch (...) { // The...

View Article


Answer by Philip Couling for Does C++ support 'finally' blocks? (And what's...

why is it that even managed languages provide a finally-block despite resources being deallocated automatically by the garbage collector anyway?Actually, languages based on Garbage collectors need...

View Article

Answer by Mephane for Does C++ support 'finally' blocks? (And what's this...

Sorry for digging up such an old thread, but there is a major error in the following reasoning:RAII moves the responsibility of exception safety from the user of the object to the designer (and...

View Article

Answer by Unhandled exception for Does C++ support 'finally' blocks? (And...

try{ ... goto finally;}catch(...){ ... goto finally;}finally:{ ...}

View Article

Answer by Martin York for Does C++ support 'finally' blocks? (And what's this...

In C++ the finally is NOT required because of RAII.RAII moves the responsibility of exception safety from the user of the object to the designer (and implementer) of the object. I would argue this is...

View Article


Answer by Michael Burr for Does C++ support 'finally' blocks? (And what's...

Beyond making clean up easy with stack-based objects, RAII is also useful because the same 'automatic' clean up occurs when the object is a member of another class. When the owning class is destructed,...

View Article

Answer by SmacL for Does C++ support 'finally' blocks? (And what's this...

FWIW, Microsoft Visual C++ does support try,finally and it has historically been used in MFC apps as a method of catching serious exceptions that would otherwise result in a crash. For example;int...

View Article


Answer by Kevin for Does C++ support 'finally' blocks? (And what's this...

No, C++ does not support 'finally' blocks. The reason is that C++ instead supports RAII: "Resource Acquisition Is Initialization" -- a poor name† for a really useful concept. The idea is that an...

View Article

Does C++ support 'finally' blocks? (And what's this 'RAII' I keep hearing...

Does C++ support 'finally' blocks?What is the RAII idiom?What is the difference between C++'s RAII idiom and C#'s 'using' statement?

View Article

Browsing all 17 articles
Browse latest View live


Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>