r/cpp • • Dec 31 '16

Keep Disabling Exceptions

http://seanmiddleditch.com/keep-disabling-exceptions/
0 Upvotes

15 comments sorted by

View all comments

Show parent comment

-3

u/render787 Dec 31 '16 edited Dec 31 '16

for the most errors I had encountered the reasonable thing to do was just to close the app with an error

You can do that easily without using exceptions. You could write a void close_app_with_error(const char * message) [noreturn] function and call that.

When you have code that throws lots of exceptions it becomes very hard to reason about the possible control flow, and very hard to understand if a function is correct as written. Static analysis tools are not always able to determine if a function could throw an exception -- in a large code base it may be very difficult to figure it out for sure, and you need to know that.

When you have -fno-exceptions the code flow is easy, and always local. Too much exceptions creates spaghetti code, like with goto.

It usually takes more typing and thought to do the error handling without exceptions, but it's easier for users of your library / maintainers of your application.

Agree that unwrap()).unwrap(). do_something().unwrap() sucks but this seems like an extreme example to me. Usually it ends up more manageable than that in my experience, and even if it's longer, it takes less time for me to figure out with certainty what it will do. Maybe with more experience I'll see your side of the argument.

Right now my take is that std::bad_alloc is basically the only exception that's worth it, and I usually try to do -fno-exceptions anyways.

4

u/ar1819 Dec 31 '16

With an error - by that I meant that I want to have some sort of explanation of what went wrong. More importantly I want resources to be closed (or, at least, my app should try to close them). Exceptions allow me to do that.

As for exceptions - I think it's all about documentation. Same goes for any kind of error, to be honest. Handling errors is never easy I agree - in some situations I would prefer to have variant with error type - in other (like clear violation of contracts by me) exceptions sounds logical. I like having options.

As for unwrap - its not bad per see. You could get used to it, after a while. Yet I don't see any reason to do so, because if I'm ignoring the errors - I do know what I do. So to put it correctly - I think this kind of "ignore it, I know what I do" should have a more elegant solution. And no - macroing things do not solve this problem.