3 ms·
I was always told c++ is fine just don’t use exceptions
by taf2 8y ago
I was always told c++ is fine just don’t use exceptions
- corysama 8y agoSomeone smarter than me once pointed out that exceptions are great as long as you don't have to worry about rolling back state. So, they are a great match for pure functional, stateless languages. But, the more procedural the language, the more complicated exceptions become. C++ can be very procedural and I'm of the opinion that I'm not smart enough to use exceptions in C++. Instead, I'm playing with Expected and monadic error handling.
- 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.