try/catch type constructs?
| From: | Jon Drukman | Date: | Fri, 01 Feb 2002 17:34:52 +0000 |
| Subject: | try/catch type constructs? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-83151@lists.php.net to get a copy of this message | ||
one thing i really love in perl is the block eval {} structure, which is similar to try/catch/throw in java and other languages.
i'm missing it a lot in php. is there a way to simulate it?
for instance, say i have a bunch of database commands. in perl, i would do something like (pseudocode):
eval {
db_connect() or die "connection failed";
set auto raise errors on db connection;
select statement;
insert statement;
whatever...
and so on...
};
if ($@) { print "db error in block: $@" }
the idea being that if any of the commands in the block fail, the database package automatically does a die("") and the block is terminated.
the nearest i could get with php is:
do {
mysql_query("UPDATE ...");
if (mysql_error()) { $error.="Couldn't update: ". mysql_error(); break; }
mysql_query("SELECT ...");
if (mysql_error()) { $error.="Couldn't select: ". mysql_error(); break; }
and so on....
} while (0==1);
if ($error) { do error stuff }
else { it worked }
it's ugly and there's a lot of extra checking of mysql_error and the like.
has anybody figured out a better way?
-jsd-