3 ms·
There is a right way to do this using RAII, and it works for files and for any other resource that might fail on close: you simply explicitly do the close yours
by jjnoakes 4y ago
There is a right way to do this using RAII, and it works for files and for any other resource that might fail on close: you simply explicitly do the close yourself, but only along the success/happy path. That way you get the best of both worlds: automatic clean-up (ignoring errors) along error paths, and proper close-and-check-for-errors along the happy path.
Of course it'd be even nicer if the implicit RAII close could somehow add its error to the error you are currently propagating, but that's beside the point for your example.