The first example, to me, is an example of how not to use try-catch. To me, you shouldn't be doing a return 0 within a catch. You should be rethrowing some error after doing what you needed to do. The only return statement should be as a result of success. Regardless of memory leaks, that's just poor practice.
I think that's a rather broad statement. Many people (myself included) think that multiple-returns can make code a lot clearer in some circumstances. For example, while you could rewrite this with a single return
sub find {
my ($self, $search_item) = @_;
eval {
# skip expensive search if item not stored anywhere
return unless $search_item->stored;
# otherwise do expensive search
$self->first;
while (my $item = $self->next) {
return 1 if $item == $search_item;
};
return;
}; if ($@) {
... handle exceptions here ...
};
};
I would argue that it would be harder to grok than the version above.
For me the main problem with Error is the fact that wrapping a bit of code in a catch block shouldn't change its semantics. This can lead to really odd behaviour when fiddling with exception based code.
-
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.
|