Re: Try Catch
| From: | (Oleg Rekutin) | Date: | Sat, 21 Jul 2001 16:12:35 +0000 |
| Subject: | Re: Try Catch | ||
| References: | 1 2 3 4 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-906@lists.php.net to get a copy of this message | ||
cox@idecnet.com (Tomas V.V.Cox) wrote in
news:3B5973E6.40F825ED@idecnet.com:
> Oleg Rekutin wrote:
>>
> With this simple code the problem can be solved. "Try" this:
>
> <?php
> require_once 'DB.php';
> // use (&$mixed) when Zeev fix the problem
> // with objects by reference :)
> function try($mixed) {
> if (DB::isError($mixed)) {
> $GLOBALS['DB_exeption'] = $mixed;
> return false;
> }
> return true;
> }
> function mydie() {
> $error = &$GLOBALS['DB_exeption'];
> if (!empty($error)) {
> die ($error->getMessage()."\n".$error->getDebugInfo());
> }
> }
>
> $dsn = 'pgsql://postgres@localhost/sexogratis';
> try($db = DB::connect($dsn)) or mydie ();
> echo "connected\n";
> try($id = $db->nextId('sequences')) or mydie();
> echo "seq id: $id\n";
> ?>
>
> try() will catch the error, store it in a global var and return false.
> This is detected by the "or" condition and call mydie() that will raise
> the catched error and finally abort the execution.
>
Well, this doesn't solve the problem (problem w/ callbacks), it avoids it
and just coats the original issue in a different kind of sugar. :) Your
try/mydie is basically a shorter way of writing
$result = expr;
if (DB::isError($result)) { die(...getmessage..etc...); }
except 'or' cannot contain a number of statements (unlike if), so you're
forced to pass information around thru $GLOBALS instead of referring to the
same return value in the same "symbol table".
> Perhaps the best solution could be to provide a system whithin all DB
> functions that use internal DB functions don't use the global error
> handler and treat the error by them selves. But I think that only very
> few functions are affected by this problem (only nextID/createSequence
> ?).
That should be the case, I think. I would consider the current callback-
related problem with nextID/createSequence a bug.
- Oleg