Re: Try Catch
| From: | (Oleg Rekutin) | Date: | Sat, 21 Jul 2001 00:03:27 +0000 |
| Subject: | Re: Try Catch | ||
| References: | 1 2 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-901@lists.php.net to get a copy of this message | ||
ed@belsoft.vitebsk.by (Ed V. Bartosh) wrote in
news:858zhjqjet.fsf@bartosh.org:
> /Fri, 20 Jul 2001 15:55:51 -0700/ you wrote:
>
> RK> Do we have a Try Catch in PHP?
>
> RK> I'm tired of
> RK> if ( PEAR::isError (
No try/catch, no exceptions support, no goto to fudge it either. I've spent
a long time trying to figure out how to emulate exceptions or alleviate the
pain of if (PEAR::isError(, but failed to come up with a solution.
If PHP had support for C++-like macros (so that 'return' in a macro ends the
function in which macro was specified and not the macro itself, and so that
the macro has the same symbol table as its 'user' function), and 'goto' or
setjmp/longjmp kinds of operators, it would be possible to have basic
exception support emulated.
> Use callbacks instead:
> PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, ...
Callbacks are not enough because they are triggered when the error is first
thrown and cannot alter the control flow. This means that the code cannot
add extra information as the error 'bubbles up' (small concern, but still
useful) or handle errors selectively (say DB raises one error that certain
code can fix, but the rest of DB errors are lethal... with a general
callback, once the error is thrown, the code that was able to fix the
problem will not be reached).
Perhaps that last point was unclear... Example, emulated MySQL sequences in
PEAR DB. If sequence was never created, attempting to retrieve the next id
results in a DB_Error thrown inside of DB_mysql::nextId(). However, nextId()
checks whether the error is 'no such table' and if so, attempts to create
the sequence, and then tries again to retrieve the next ID. If a callback is
registered, then the execution will stop when nextId() fails to obtain the
first ID, even though it knew how to recover.
In my current application, I just made a keyboard shortcut for if
(PEAR::isError($result)) return $result, which is what I do in almost all
cases, with the exception of where I actually handle received errors.
- Oleg