Some reasons I like exceptions:
Robustness. I can forget to check for an returned error value. I cannot forget to check for an exception.
Brevity. I prefer:
$o->foo or return(ERROR_FOO);
$o->bar or return(ERROR_BAR);
$o->fribble or return(ERROR_FRIBBLE);
$o->ni or return(ERROR_NI);
Clarity. With exception based code the "normal" flow of control is more explicit because it is not obscured by error handling code. I think that the first of the two code examples above shows the intent of the code more directly than the second does.
Separation of concerns. The error condition and the error handler are different ideas.
- You may want an error to be handled in different ways depending on the context.
- You may also not know how the error should be handled at the point it occurs.
- You may not know how the error should be handled at the time you write the code.
With the return-error-code style you end up having to either:
- propogate error conditions to where the decision on how they should be handled can be made.
- propogating error handlers down to where the errors may occur
Both options rapidly become messy if there are many levels of code between the error condition and the error handler.
No confusion between return values and error conditions.
There are probably some more ;-)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||
Others imbibing at the Monastery:
(12)As of 2017-01-24 20:47 GMT