|
|
|
|
|
by agent327
1747 days ago
|
|
That second program must be the single worst example of exceptions ever written; a straw man if ever these was one. The key in understanding why this is the case lies in the realisation that ParseInt is a combined parser/validator, and a validation failure isn't actually an error; it's a normal, expected, situation. In C++, you'd solve this by returning std::optional<int>, and end up with code like this: std::optional<int> result = ParseInt (input, 8);
if (!result) result = ParseInt (input);
if (!result) result = ParseInt (input, 16);
if (!result) throw ...;
return *result;
Note how there's no exceptions (in ParseInt, I mean). Note how there's no error codes either. There's just no error handling needed to begin with, except right at the end, if the number is not in any of the three supported formats. |
|
I would argue that if (!result) is a form of error handling, as result being falsy indicates that the parsing failed.