4 ms·
Exceptions are not for handling programmer errors. Assertions are. Exceptions are indeed used for handling exceptional situations such as actual errors. If you
by AstralStorm 9y ago
Exceptions are not for handling programmer errors. Assertions are.
Exceptions are indeed used for handling exceptional situations such as actual errors. If you only want to copy a file it it exists, then check if it exists (race-prone but fast) or create a function that copies a file only if it exists.
Or you know, swallow the nonexistence exception. An optimizing compiler might be able to elide the throw statement if it is inlining the copy function.
The only issue seems to be that few people are able to install or create a good top level error handler in case there is an actual unhandled failure.
- mehrdadn 9y ago> Exceptions are not for handling programmer errors. Assertions are. std::logic_error?
- AstralStorm 9y agoThat is not a programmer error, this is a domain error mostly for math functions or string parsing. (Other than relatively evil std::length_error/out_of_range which might go either way.) In most cases where it is a bit gratuitously thrown it can be avoided. You might want as well to redefine any error as a programmer error.
- mehrdadn 9y ago> That is not a programmer error, this is a domain error mostly for math functions or string parsing. ....what in the world? no... for domain errors there's, erm, std::domain_error. C++ Reference/StackOverflow/MSDN/etc. already explain all these. But it seems I have to copy-paste them here? "Domain and range errors are both used when dealing with mathematical functions." [1] "std::logic_error reports errors that are a consequence of faulty logic within the program such as violating logical preconditions or class invariants and may be preventable." [2] "std::domain_error may be used by the implementation to report domain errors, that is, situations where the inputs are outside of the domain on which an operation is defined." [3] [1] https://stackoverflow.com/a/641972 https://stackoverflow.com/a/641972 [2] http://en.cppreference.com/w/cpp/error/logic_error http://en.cppreference.com/w/cpp/error/logic_error [3] http://en.cppreference.com/w/cpp/error/domain_error http://en.cppreference.com/w/cpp/error/domain_error
- blub 9y agoSure they are :) I think that, together with std::optional and futurestd::expected could be a very robust error handling strategy for C++.