in reply to Re^2: Can someone please write a *working* JSON module
in thread Can someone please write a *working* JSON module
It doesn't have to be a dichotomy. Exceptions can be used when you need to fail the operation and jump in an overall exception handler multiple functions above in the call stack. Success/error return values (preferably when they are of different types and the compiler can make sure you're not using the success type in the error branch, a.k.a. the Optional<Success, Failure> idiom) can be used when you can check for and handle the error right away.
People who use exceptions won't argue in favour of code like the following:
That's just more typing for the same result aseval { step_1(); 1; } or handle_error_1(); eval { step_2(); 1; } or handle_error_2(); eval { step_3(); 1; } or handle_error_3(); eval { step_4(); 1; } or handle_error_4();
step_1() or handle_error_1(); step_2() or handle_error_2(); step_3() or handle_error_3(); step_4() or handle_error_4();
On the other hand, if an error means the whole operation should be aborted and the individual steps signal that by raising an exception, we can write it as
and still be sure that when the error happens, (1) any resources will be cleaned up safely by desctructors and (2) the following steps won't be taken.try { step_1(); step_2(); step_3(); step_4(); } catch { handle_overall_error(); };
By the way, why do you dislike eval? I mean, I don't like eval EXPR either, but what's wrong with eval BLOCK?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Can someone please write a *working* JSON module
by bliako (Abbot) on Oct 26, 2021 at 09:17 UTC | |
by dave_the_m (Monsignor) on Oct 27, 2021 at 10:47 UTC | |
by bliako (Abbot) on Oct 27, 2021 at 16:17 UTC | |
by Anonymous Monk on Oct 27, 2021 at 06:17 UTC | |
by bliako (Abbot) on Oct 27, 2021 at 07:58 UTC |