in reply to Re: How to say 'oops' in OOPs?
in thread How to say 'oops' in OOPs?

Thanks go to all. I think I'll go with the blank return;/$obj->errstr approach. I can see why one might do the die/eval thing, but it's not really my flavour of doing things.

And yes, there is documentation.

--
bowling trophy thieves, die!

Replies are listed 'Best First'.
Re: Re: Re: How to say 'oops' in OOPs?
by Anonymous Monk on Apr 08, 2003 at 20:21 UTC
    If the error is really unexpected, don't be afraid to croak or die. eval/die is comparable to try/throw in other languages. If the error is expected to occur, then undef is probably the best returned value.

    Personally, I think "return undef;" is more clear to the reader than "return;" even though they have an identical effect. Explicitly returning undef tells the reader that hey, dammit, the caller is probably watching for a false/undef return.

      Personally, I think "return undef;" is more clear to the reader than "return;" even though they have an identical effect.

      Actually, they're not identical ;-)

      A raw return will return an empty list in a list context and undef in scalar context. This can make writing code that needs to behave itself in either context easier. See return for more info.

      As adrianh said, a blank return actually equates to
      return wantarray ? () : undef;
      or probably more correctly
      return !defined(wantarray) || wantarray ? () : undef;

      Makeshifts last the longest.

Re^3: How to say 'oops' in OOPs?
by adrianh (Chancellor) on Apr 09, 2003 at 10:46 UTC

    You can always make the error handling behaviour an option. For example, DBI can be made to return errors, or throw an exception, depending on whether you pass the RaiseError option at object creation time.

    That way you can keep exception throwing freaks like me happy too :-)

    You might also find Best Practices for Exception Handling of interest.

      That's a most useful reference; thank you. If I'd have found it first (and, to be frank, if Toronto Perl Mongers list management code hadn't barfed on my original message just 'cos it had sub in it) I might not have needed to ask. Mind you, the discussion here has taken interesting and useful (to me) directions.

      --
      bowling trophy thieves, die!