There are many different opinions on this. In Python circles, for example, there’s a truism that “it’s easier to ask forgiveness than permission,” so a widely accepted practice is to “just do it,” relying upon an exception (of a particular class) being thrown if it turns out that you can’t. Exceptions are thrown in all sorts of benign circumstances, including “to end an iterator-loop.” Ordinarily, code which uses the try..except..finally constructs check for a particular exception-class or classes, intercepting only those that are expected. (As expected, exceptions which are never caught by anyone typically kill the program.)
And it turns out that Perl can do this, too. The die() function can accept any object, not just a string, and there are several “exception class hierarchy” packages available to exploit this capability.
As it happens, I find myself using “classed exceptions” frequently, throwing exceptions in basically all of the situations where I might previously have used “a non-zero return code,” but also for benign purposes. It’s much too late to change the name of Perl’s die() function, as if we ever would want to, but it is a bit misleading. You are raising, or “throwing,” an exception ... which may or may not be “a bad thing.”
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|