in reply to Re4: Learning how to use the Error module by example
in thread Learning how to use the Error module by example
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re7: Learning how to use the Error module by example
by dragonchild (Archbishop) on Jul 30, 2003 at 13:30 UTC | |
by adrianh (Chancellor) on Jul 30, 2003 at 13:50 UTC | |
by dragonchild (Archbishop) on Jul 30, 2003 at 14:15 UTC | |
by adrianh (Chancellor) on Jul 30, 2003 at 15:20 UTC | |
by dragonchild (Archbishop) on Jul 30, 2003 at 15:38 UTC | |
|