Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Error codes vs die exceptions vs ...

by haj (Priest)
on Jul 05, 2019 at 22:11 UTC ( #11102463=note: print w/replies, xml ) Need Help??


in reply to Error codes vs die exceptions vs ...

In my opinion, the relevant question is: What information do you need to pass so that someone (not necessarily the direct caller) can fix the error?

With a return code, the direct caller must check the result, but with an exception he can chose to do it, but somebody up the calling hierarchy ought to do it. However, this difference vanishes if the direct caller either can actually handle the error by itself, or needs to add diagnostics to make the exception meaningful to his callers. In both these cases she would need to catch the exception, which requires more typing than checking a return code. Exceptions are needed as a method to get "out of band" only in the (exotic, IMHO) case where every value, including undef, is a valid return value.

Some prominent CPAN modules (e.g. DBI and Template) solve the problem of passing diagnostics by returning undef on error, and then providing a special method to retrieve details. The open function uses undef and $! for the same purpose, and can serve as a bad example because of insuffient diagnostics: "No such file or directory" doesn't tell us what the operation was, nor what path was tried (autodie nicely fixes that).

Good interfaces can be designed either way. Bad interfaces, too. Every level of error handling in the call stack can chose to convert an exception to a return code or vice versa.

  • Comment on Re: Error codes vs die exceptions vs ...

Replies are listed 'Best First'.
Re^2: Error codes vs die exceptions vs ...
by LanX (Sage) on Jul 06, 2019 at 12:53 UTC
    > What information do you need to pass so that someone (not necessarily the direct caller) can fix the error?

    Thanks for nailing it.

    In the demonstrated case the direct caller must react and the details of the error are only relevant for STDERR.

    And it doesn't stop me catching fatals on a higher call level with try/catch.

    > only in the (exotic, IMHO) case where every value, including undef, is a valid return value.

    Please note that that's not a problem in the demonstrated model with list assignments.

    One can safely return undef.

    I.e. only an empty return counts as error.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11102463]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2022-01-21 12:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (57 votes). Check out past polls.

    Notices?