Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Best Practices for Exception Handling

by Anonymous Monk
on Jan 29, 2003 at 00:17 UTC ( #230811=note: print w/ replies, xml ) Need Help??


in reply to Best Practices for Exception Handling

I've always used perl's EVAL command: my $val;

eval {
 my $val = $self->do_command();
};

warn " do_command failed with the following error \n\n@_" if ("@_" ne "");

- adam


Comment on Re: Best Practices for Exception Handling
Replies are listed 'Best First'.
Re^2: Best Practices for Exception Handling
by particle (Vicar) on Jan 29, 2003 at 01:13 UTC

    my method is similar, but different...

    ## let's say bar is a method in foo sub bar { my( $self, @args )= @_; eval { $self->method( @args ) }; $@ and $self->raise_error( @_ ) and return undef; } ## called like: my $foo= foo->new(); my $result= $foo->bar( @args ) or warn $foo->Error();

    the method sets an error and returns undef, allowing the caller to deal with the error as it sees fit. of course, you might want to handle a method that can return 0 by using defined

    ~Particle *accelerates*

      of course, you might want to handle a method that can return 0 by using defined
      Or use the trick that some other modules use: return "0E0" or "0 but true" for zero, as a string. These are both true and 0, and do not produce warnings when converted into a number, the former because it's a normal floating point format, the latter because it's a hardcoded exception in perl.

      Now I come to think of it: you can use any normal format for zero, as long as it's not "0", for example "0.0".

      Cool.. that's the way I like to handle errors too; but I always wonder if it's the *best* way. This is an interesting thread - Ovid++

      Jon

      Personally I think the following little trick/modification makes for cleaner code... (and I think that raise_error makes more sense if it contains the error message returned...)
      sub bar { my( $self, @args )= @_; eval { $self->method( @args ); 1} or $self->raise_error( $@, @_ ) and return undef; }
      If you can contrive to make raise_error() return undef you can make it even cleaner
      sub bar { my( $self, @args )= @_; eval { $self->method( @args ); 1} or return $self->raise_error( $@, @_ ); }

      --- demerphq
      my friends call me, usually because I'm late....

Re: Re: Best Practices for Exception Handling
by theguvnor (Chaplain) on Jan 29, 2003 at 01:05 UTC

    warn " do_command failed with the following error \n\n@_" if ("@_" ne "");

    Of course you meant $@, right? ;-)

    Jon

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (13)
As of 2015-07-31 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (276 votes), past polls