3 ms·
Exceptions are harder to analyze because they create scopes from which you are already "handling" errors, but if you add code to the scope that throws a new err
by BlackFly 6y ago
Exceptions are harder to analyze because they create scopes from which you are already "handling" errors, but if you add code to the scope that throws a new error of that type your handler may not actually be able to handle it.
try {
someCall();
someOtherCall();
} catch (ErrorType err) {
//handle
}
Which method throws? If they both throw ErrorType but one of them cannot be handled, then you have to put additional logic into the catch and rethrow.
This is a common source of faulty error handling.
- simiones 6y agoYes, this is an easier mistake to make with exceptions than with error values. Still, the fix is to do exactly the same thing as in the error values case: try { someCall() } catch (ErrorType err) { //handle one way } try { someOtherCall() } catch (ErrorType err) { //handle another way } It's no less verbose than the error values way, though again, it is easier to make the mistake in the first place. I'm not trying to claim that exceptions are ultimately better than error values, just that the difference isn't so much "non-local control flow" as it is explicitness vs implicitness. Exceptions are implicit, error codes are explicit. Both have benefits and drawbacks.