3 ms·
I don't get it: why use a cumbersome try/catch block to handle an error in the same context when one could just use the so-called "C equivalent": ... if (c
by chombier 8y ago
I don't get it: why use a cumbersome try/catch block to handle an error in the same context when one could just use the so-called "C equivalent":
...
if (condition1)
handle_exception1 ();
...
if (condition2)
handle_exception2 ();
...
As far as I understand, the essence of exceptions is to defer error handling to the caller, so local try/catch-es are examples of exception misuses.
- m_mueller 8y agomaybe the thinking is that since library functions that you use can also throw exceptions, you should also do that in order to not mix error handling. I don't really get it either though. I just like C more because of its restrictions, and I consider restrictions a virtue if they point you in the right directions (e.g. good and predictable performance).
- sclangdon 8y agoWhilst I agree with you that this isn't the best use of exceptions, one reason to use them this way might be because exceptions (generally) don't cost anything on the success path. if statements do. Of course exceptions cost a lot more on the failure path, but that's not generally the priority.
- Ragnarork 8y ago> local try/catch-es are examples of exception misuses. Not in the context of RAII. It helps a lot with cleanup and ensuring nothing leaks from the snippet that caused the exception if any resources were acquired. Whereas in C you'd have to make all that cleanup manually, which can be tedious and error prone.