Hacker News new | past | comments | ask | show | jobs | submit login

> 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 :-)




C# will let you do something similar with the using statement.


but using "using" in C# has to be an explicit action from the programmer. In C++ it's the default behaviour for new values.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: