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.
In reply to Re^6: Learning how to use the Error module by example
by adrianh
in thread Learning how to use the Error module by example
by cleverett
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |