3 ms·
> Someone smarter than me once pointed out that exceptions are great as long as you don't have to worry about rolling back state and the point of exceptions in
by jcelerier 8y ago
> Someone smarter than me once pointed out that exceptions are great as long as you don't have to worry about rolling back state
and the point of exceptions in C++ is that they have to be used in combination with the "rolling-back state" feature backed in the language, namely RAII. It's only a pain in language without such things such as C# / Java, but in C++ if I do
file my_file;
// .. do stuff
auto f = std::make_unique<whatever>(...);
// ...
throw some_error;
I know that my_file will be closed if it had been opened, and that my memory will be freed.
Of course if your code is littered with `new` and `fopen` left and right that won't work, but the code won't pass code review in the first place :-)
- tonyedgecombe 8y agoC# will let you do something similar with the using statement.
- jcelerier 8y agobut using "using" in C# has to be an explicit action from the programmer. In C++ it's the default behaviour for new values.