Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Learning how to use the Error module by example

by perrin (Chancellor)
on Jul 28, 2003 at 21:14 UTC ( #278624=note: print w/ replies, xml ) Need Help??


in reply to Learning how to use the Error module by example

I recommend that you don't use the try/catch syntax of the Error module. It can lead to some difficult bugs involving closures and returns that don't actually return.


Comment on Re: Learning how to use the Error module by example
Re2: Learning how to use the Error module by example
by dragonchild (Archbishop) on Jul 29, 2003 at 12:31 UTC
    Please provide an example of where this situation occurs. If I didn't know you better, perrin, I would say that your statement is one of an ex-Java programmer waving his hands cause the try-catch syntax in Error reminds him of dark days in the past. :-)

    ------
    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.

      I wouldn't just go spreading FUD abour Error.pm. It has very serious problems, and the only reason I didn't describe them is that I thought they were well-known. The try/catch syntax is implemented using sub ref prototypes. Take a look at this code:
      use Error qw( :try ); sub important_function { try { something_dangerous(); } catch Error::Simple with { return 0; }; return 1; }
      What value do you think this sub will return if it catches an error? It will always return 1, because the return inside of the catch block just returns from the implicit subroutine that the catch block creates. Nasty.

      Possibly worse, the nested closure problem can cause memory leaks in a long-running process. The problem is partially described by Matts in this presentation. Here's an example of a sub that will leak memory every time you call it:

      use Error qw( :try ); sub leaky_function { my $foo = 7; try { # do something here try { $foo++; } catch Error::Simple with { # whatever }; } catch Error::Simple with { # whatever }; }
      I hope I've got this code right; it's been a while since I used it.

      UPDATE: This leak seems to have been fixed in recent versions of Perl. That's a nice thing to see!

      We used Error in the system we built at eToys, and after having both of the problems I descibe here I have decided never to use the try/catch syntax again. It's dangerous and the problems are hard to see.

        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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2014-07-23 18:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (149 votes), past polls