http://qs321.pair.com?node_id=11102463


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.