Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Rethrowing with die $@ considered harmful

by shmem (Chancellor)
on Jul 18, 2006 at 07:56 UTC ( [id://561953]=note: print w/replies, xml ) Need Help??


in reply to Rethrowing with die $@ considered harmful

Rethrowing an exception with die $@ if (some expression involving $@) (an idiom mentioned at least 6 times in the perl documentation) has a problem when the expression involving $@ ends up doing anything that inadvertently clears $@.

I would consider inadvertently clearing $@ (or $!) as harmful, not the rethrowing.

When you ask for problems, you get them. E.g. if you overload, you should know what you are doing...

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
  • Comment on Re: Rethrowing with die $@ considered harmful

Replies are listed 'Best First'.
Re^2: Rethrowing with die $@ considered harmful
by adrianh (Chancellor) on Jul 18, 2006 at 08:04 UTC
    I would consider inadvertently clearing $@ (or $!) as harmful, not the rethrowing.

    But there are so many places where this can happen.

    eval { foo() } ; if ( $@ ) { bar(); die $@ }

    Does bar() throw an exception itself? If it doesn't now will it sometime in the future? What about the stuff that bar() calls? What about the next version of the module Foo used by module Bar used by the bar() function?

    If you rely on $@ you're relying on everything in your exception handling code restoring it before you use it. Me - I'll copy it instead. Coupling bad.

    Sure the overloading thing is daft - but that doesn't mean copying $@ isn't the right thing to do.

      I consider this code buggy. If $@ is set, it must be reported/processed immediately without any further function call. If you just can't die from it, use warn:
      eval { foo() } ; if ( $@ ) { warn $@; bar(); } Undefined subroutine &main::foo called at 561955.pl line 2. Undefined subroutine &main::bar called at 561955.pl line 5.

      Note: I'm not against copying $@. If you need to make further calls first, copy it and report it later. But inadvertently clearing is a programmer's bug, not perl's fault.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      200th post
        I consider this code buggy. If $@ is set, it must be reported/processed immediately without any further function call.

        I agree. That was my (and I believe the OPs) point!

      Does bar() throw an exception itself?

      Getting away from working with $@ directly is one reason why I wrote Exception::Class::TryCatch -- the try keyword on eval isn't just sugar, it saves away $@ onto a stack for a later catch. That way, it can even be nested. E.g.

      use Exception::Class::TryCatch; try eval { Exception::Class::Base->throw('error') }; # can eval or use try/catch again regardless of $@ above bar(); catch my $err; # catches the matching "try"

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found