nmerriweather has asked for the wisdom of the Perl Monks concerning the following question:
i make frequent use of die in eval blocks to trap errors.
my code often looks like this:
my problem lies in these 2 lines:
my issue is that if i die on "invalid user" (note, this is a simple version, in practice, there could several of these custom die items chained, i want to treat that differently : i still want to die and exit the block and return an error , but I don't want to do the logging.
can anyone suggest a way to do that and keep a similar syntax/code ? the only thing i could figure out was to have a '$supress_warning' variable before the block, and set it for custom dies -- but that means i'd have to write the dies as
my code often looks like this:
note, thats a simplified version.sub check_id { my ( $id )= @_; my $res; eval { my $obj= ObjClass::Obj->new(); $obj->id( $id ); $obj->load_via_id() ; $res= $obj->id ; die "invalid user" unless $obj->activated_account ; } if ($@) { DEBUG && log_error( $@ ); return -1; } return $res; }
my problem lies in these 2 lines:
load_via_id could die and toss an exception - at which point things work as they should. i want to catch that die.$obj->load_via_id() ; die "invalid user" unless $obj->activated_account ;
my issue is that if i die on "invalid user" (note, this is a simple version, in practice, there could several of these custom die items chained, i want to treat that differently : i still want to die and exit the block and return an error , but I don't want to do the logging.
can anyone suggest a way to do that and keep a similar syntax/code ? the only thing i could figure out was to have a '$supress_warning' variable before the block, and set it for custom dies -- but that means i'd have to write the dies as
someone out there with more experience than me must have a suggestion! i hope!if ( !$obj->activated_account ) { $suppres_warning++; die "invalid account"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: novice 'die' help requested
by friedo (Prior) on Jan 11, 2007 at 19:50 UTC | |
Re: novice 'die' help requested
by jettero (Monsignor) on Jan 11, 2007 at 20:03 UTC | |
Re: novice 'die' help requested
by muba (Priest) on Jan 11, 2007 at 21:53 UTC | |
by friedo (Prior) on Jan 11, 2007 at 22:18 UTC | |
by muba (Priest) on Jan 11, 2007 at 23:46 UTC | |
by nmerriweather (Friar) on Jan 13, 2007 at 01:03 UTC | |
Re: novice 'die' help requested
by ikegami (Patriarch) on Jan 11, 2007 at 20:35 UTC | |
by f00li5h (Chaplain) on Jan 11, 2007 at 23:21 UTC | |
Re: novice 'die' help requested
by sfink (Deacon) on Jan 12, 2007 at 17:38 UTC | |
Re: novice 'die' help requested
by TOD (Friar) on Jan 17, 2007 at 18:20 UTC |
Back to
Seekers of Perl Wisdom