top | item 21927445

(no title)

bastih | 6 years ago

> The noexcept specifier protects C++ program from memory leaks and potentially other undesired behaviors brought by throwing exceptions.

While I agree with the presented observation, just letting the exception bubble up to the top would have done the same thing. The memory leak presented here is just the result of not using RAII to clean up the resource.

Throwing an exception through a `noexcept` function terminates (or rather calls std::terminate) because you are doing something you explicitly said you wouldn't do, pass an exception through this function.

> For key functions we hardly know how to handle its exceptions, it might be a good idea to add the noexcept specifier.

On the contrary, `noexcept`s usecase is in places where we want to be sure a function doesn't just throw an exception, as this may thwart some guarantees we want to make to the caller. Sprinkling your functions with `noexcept` just to abort (which is already happening if the exception is unhandled) is bad code. Plus "we hardly know how to handle its exceptions" is the worst cop-out I've heard for dealing with exceptions - C++ exceptions are an essential part of the language and many libraries, so dealing with them should be a prime concern of the programmer.

discuss

order

yati|6 years ago

I agree with your pointa. A note though, I also used to think exceptions are "essential", but after working with code that forbids exceptions and uses explicit error status passing, I find exceptions very awkward to work with. I find them a very lazy way of doing error propagation, which in turn encourages lazy error handling.

erik_seaberg|6 years ago

C++ relies heavily on error handling using out-of-band exceptions. f(g(h())) needs h and g to return meaningful values rather than errors, constructors can't return errors at all, and member initializer lists can't check for returned errors.

You could make a dialect in which every method returns an error and every object has an init(...) method instead of a ctor, but it would be closer to imperative VB than idiomatic and readable C++.

bastih|6 years ago

Both standard library and other libs make use of them, so you often can't just ignore exceptions.

Also agree on your point - I'm not saying I like exceptions, I like explicitly passing type-encapsulated results/errors more for clarity. Exceptions happen in a "second invisible lane" right next to the code you are writing, and for example aren't immediately visible when reviewing code.

je42|6 years ago

also note that Java has checked exceptions, and a lot of programmers still opt to deal with errors in a lazy way.

Same for error codes, go has explicit error, and several programmers still opt to deal with errors in a lazy way.

I think the syntax and semantical contructs a language offers doesnt matter much when a lot of programmers dont have time and/or discipline and/or priority to deal with error cases.

Error handling is hard. Also, since programmers in general think less about errors, library often dont expose errors in helpful ways and/or well-defined ways.