Re: eval{}



On 3/6/06, Tom Allison <tallison@xxxxxxxxxxx> wrote:

I ran into a potential problem when writing some code this weekend.

I'm running a network socket to pick up data and then run it against a
database connection before I return the response. Essentially it falls
into a few steps:

read from network
read from database
write to database
do something else on network
write to network

Being the cautious code writer I thought it would make sense to use
something like:

eval{
alarm(10);
"read from network"
"read from database"
....
}

But each of these database calls has it's own eval{} around the database
query (as an example):
my $rv;
eval {
$rv = $dbh->do($sql) or die $!;
};
if ( $@) {
die "FATAL!\n$@\n"
} else {
return $rv;
}

Which essentially means if I flattened out all the subroutines I would
end up with something like

eval {
...
eval {....}
if ($@) { }
eval { ... }
if ($@) {...}
}
if ($@) {
.....}

The problem I run into is throwing the exceptions up to the top eval{}
structure when I need to communicate that something didn't work right
so I can provide feedback to the network client connection.

I suppose I could try and rewrite the code to un-nest the eval{}
statements but I think that is avoiding learning something I should know
about how to manage eval{} for block exception handling.

Is there some way to rethrow or propogate errors or some tips on how to
manage this better?


die on errors and just keep passing them up the line:

eval {
eval {
eval {
bad_system_call() or die "$!\n";
} or die $@;
} or die $@;
};

print "eval says: $@\n" if $@;

As long as you keep propagating $@ in your die calls, the original
error message will get passed out.

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org

values of β will give rise to dom!


Relevant Pages

  • Re: Audacity and Gentoo
    ... > Oracle Black Helicopter Base in Antarctica: `Hey, we've got a database, ... available, especially from Australia's ABC national radio network, ... I don't sync it with the computers, ... There's over 5,000 games in that 15 ...
    (uk.comp.os.linux)
  • (no subject)
    ... - LDB locking which a persistent recordset connection fixes ... New format of Access 2000 MDB ... Place backend MDB on the root of the network share rather than several folders down ... Subdatasheets are a new feature in Access 2000 Therefore, you are more likely to notice this behaviour after you convert a database from an earlier version. ...
    (comp.databases.ms-access)
  • Re: Replication/Sychronization and security
    ... When put on the network it takes several seconds for those close to ... I am told that our network drives are for data storage (word/excel ... explore the possiblity of replication as a solution. ... >> database to decrease the potential for breach. ...
    (microsoft.public.access.replication)
  • Re: Remote data entry solution needed for Access 2007
    ... Users open the database remotely and use its forms to enter data. ... Why it doesn't work: network lag time. ... our network admin says our Citrix server is 2000 and ... HTML-based form working with an ASP file to send data to the database. ...
    (microsoft.public.access.externaldata)
  • Re: Database Performance Problem between 3:00PM and 4:00PM
    ... where by database becomes extremely slow between 3:00PM and 4:00PM. ... When one looks at CPU Usage, Memory Usage and Disk Usage nothing ... FRom the beginning we suspected Network to be an issue, ... Is it possible that an Oracle backup job, export, or ...
    (comp.databases.oracle.server)