Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re4: Learning how to use the Error module by example

by dragonchild (Archbishop)
on Jul 29, 2003 at 17:51 UTC ( [id://278907]=note: print w/replies, xml ) Need Help??


in reply to Re: Re2: Learning how to use the Error module by example
in thread Learning how to use the Error module by example

Excellent! ++! Now, that explains what's going on. It's a little worrying that these examples are not in the Error documentation. A few thoughts:

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.

The second method ... the closure concern is a bit harsher for me. Matts does provide a solution for it, but I don't like it as it removes the major benefit imho, which is the syntactic sugar. I'll have to think about it and see whether it has a good workaround.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re^6: Learning how to use the Error module by example
by adrianh (Chancellor) on Jul 29, 2003 at 20:40 UTC
    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.

      My point is that if you're wrapping in a try-block, you should be throwing exceptions, even something like Error::OK or the like. In fact, I wouldn't even call them exceptions, really. They're more like signals within the same process. Just because we use them 99.999% of the time to indicate an abnormal situation doesn't mean that this is what they should be limited to ...

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        In the find() example, assuming that stored(), next() and first() can throw error exceptions, show me some simpler code using exceptions rather than multiple returns.

        I've no objection to using exceptions for non-error conditions. I do it all the time :-)

        However, I do have an objection to making my code more complex because of an arbitrary rule. Why should I not use return within a try block when it makes my code far easier to understand and maintain than the alternative? What is the downside?

Re: Re4: Learning how to use the Error module by example
by perrin (Chancellor) on Jul 29, 2003 at 18:05 UTC
    My return example is contrived, but there are situations where one might want to return from within a try block and it's counterintuitive to have to avoid it. This problem came up in the course of normal use. We were not looking for trouble.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://278907]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (9)
As of 2024-03-28 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found