# parse errors when testing for undefined exceptions

- Thread: https://externals.io/message/12523
- Original archive: https://news-web.php.net/php.internals/12523
- Started by Alan Knowles on 2004-09-01T13:04:03+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #12523 — Alan Knowles — 2004-09-01T13:04:03+00:00

URL: https://externals.io/message/12523#12523

This is a simple example of why making a parse error out of undefined 
Exception types is going to be very problematic.

function test($a) {

    if (!extension_exists('sqlite')) {
        return;
    }
    try {
        SQLite::query($a);
        
    // parse error!!! - if we dont have sqlite, we dont have SQLite 
exception!

    } catch(SQLite_Exception $e) {

        echo "problem with query";
        return;
    }
}


This has a big knock on effect that we can not lazy load Exception 
definitions, even if they are only used in Exceptional situations. (its 
pretty much the same issue as instanceof - forcing the loading of code, 
that may never be used, except to test it's non-existance.)

Regards
Alan

---

## #12524 — Alan Knowles — 2004-09-01T13:22:24+00:00

URL: https://externals.io/message/12523#12524 · In reply to: #12523

aghh.. - another crap example:

The priciple it was try to make was that

....
    require_once 'MyException.php'
    throw MyException(....);
....

then trying to catch MyException leads to a parse error when it's 
try()'d and not loaded.

Regards
Alan

Alan Knowles wrote:

> This is a simple example of why making a parse error out of undefined 
> Exception types is going to be very problematic.
>
> function test($a) {
>
>    if (!extension_exists('sqlite')) {
>        return;
>    }
>    try {
>        SQLite::query($a);
>           // parse error!!! - if we dont have sqlite, we dont have 
> SQLite exception!
>
>    } catch(SQLite_Exception $e) {
>
>        echo "problem with query";
>        return;
>    }
> }
>
>
> This has a big knock on effect that we can not lazy load Exception 
> definitions, even if they are only used in Exceptional situations. 
> (its pretty much the same issue as instanceof - forcing the loading of 
> code, that may never be used, except to test it's non-existance.)
>
> Regards
> Alan

---

## #12525 — Alan Knowles — 2004-09-01T13:23:23+00:00

URL: https://externals.io/message/12523#12525 · In reply to: #12523

aghh.. - the previous example was crap

The priciple it was try to make was that

....
    require_once 'MyException.php'
    throw MyException(....);
....

then trying to catch MyException leads to a parse error when it's 
try()'d and not loaded. means we cant lazy load any Exceptions, and save 
file io/memory etc.

Regards
Alan

Alan Knowles wrote:

> This is a simple example of why making a parse error out of undefined 
> Exception types is going to be very problematic.
>
> function test($a) {
>
>    if (!extension_exists('sqlite')) {
>        return;
>    }
>    try {
>        SQLite::query($a);
>           // parse error!!! - if we dont have sqlite, we dont have 
> SQLite exception!
>
>    } catch(SQLite_Exception $e) {
>
>        echo "problem with query";
>        return;
>    }
> }
>
>
> This has a big knock on effect that we can not lazy load Exception 
> definitions, even if they are only used in Exceptional situations. 
> (its pretty much the same issue as instanceof - forcing the loading of 
> code, that may never be used, except to test it's non-existance.)
>
> Regards
> Alan

---

## #12526 — Ron Korving — 2004-09-01T13:34:33+00:00

URL: https://externals.io/message/12523#12526 · In reply to: #12523

in your function, you should assume sqlite is present.
before calling any functions, have one of the first actions in your script
(preferably in some file you include always anyway) be a check on the
existance of sqlite.

problem solved?

"Alan Knowles" <alan@akbkhome.com> schreef in bericht
news:4135C8C3.5000705@akbkhome.com...
> This is a simple example of why making a parse error out of undefined
> Exception types is going to be very problematic.
>
> function test($a) {
>
>     if (!extension_exists('sqlite')) {
>         return;
>     }
>     try {
>         SQLite::query($a);
>
>     // parse error!!! - if we dont have sqlite, we dont have SQLite
> exception!
>
>     } catch(SQLite_Exception $e) {
>
>         echo "problem with query";
>         return;
>     }
> }
>
>
> This has a big knock on effect that we can not lazy load Exception
> definitions, even if they are only used in Exceptional situations. (its
> pretty much the same issue as instanceof - forcing the loading of code,
> that may never be used, except to test it's non-existance.)
>
> Regards
> Alan

---

## #12527 — Ron Korving — 2004-09-01T13:36:49+00:00

URL: https://externals.io/message/12523#12527 · In reply to: #12525

Okay, I agree with you on this one.
Very weird behaviour by the way.. I didn't know about this.

"Alan Knowles" <alan@akbkhome.com> schreef in bericht
news:4135CD4B.6040508@akbkhome.com...
> aghh.. - the previous example was crap
>
> The priciple it was try to make was that
>
> ....
>     require_once 'MyException.php'
>     throw MyException(....);
> ....
>
> then trying to catch MyException leads to a parse error when it's
> try()'d and not loaded. means we cant lazy load any Exceptions, and save
> file io/memory etc.
>
> Regards
> Alan
>
> Alan Knowles wrote:
>
> > […]

---

## #12532 — Marcus Boerger — 2004-09-01T19:56:25+00:00

URL: https://externals.io/message/12523#12532 · In reply to: #12523

Hello Alan,

juts for the record, you can even __autoload the missing exception class:

php -n -r 'function __autoload($n) { var_dump($n); eval("class $n extends Exception {}"); } try { } catch(ExcetpionXYZ $e) {};'

and besides what should the compiler do? Guess what that exception you specified
might be? That's like expecting functions were present of the not loaded
extension...

regrads
marcus

Wednesday, September 1, 2004, 3:04:03 PM, you wrote:

> This is a simple example of why making a parse error out of undefined 
> Exception types is going to be very problematic.

> function test($a) {

>     if (!extension_exists('sqlite')) {
>         return;
>     }
>     try {
>         SQLite::query($a);
        
>     // parse error!!! - if we dont have sqlite, we dont have SQLite 
> exception!

>     } catch(SQLite_Exception $e) {

>         echo "problem with query";
>         return;
>     }
> }


> This has a big knock on effect that we can not lazy load Exception 
> definitions, even if they are only used in Exceptional situations. (its 
> pretty much the same issue as instanceof - forcing the loading of code, 
> that may never be used, except to test it's non-existance.)

> Regards
> Alan




-- 
Best regards,
 Marcus                            mailto:helly@php.net

---

## #12538 — Alan Knowles — 2004-09-02T03:16:27+00:00

URL: https://externals.io/message/12523#12538 · In reply to: #12532

Marcus Boerger wrote:

>Hello Alan,
>
>juts for the record, you can even __autoload the missing exception class:
>
>php -n -r 'function __autoload($n) { var_dump($n); eval("class $n extends Exception {}"); } try { } catch(ExcetpionXYZ $e) {};'
>  
>
dont you think this is a really horrible kludge?

>and besides what should the compiler do? Guess what that exception you specified
>might be? That's like expecting functions were present of the not loaded
>extension...
>  
>
if (!function_exists("hello_world")) {
 ....
}

While I can understand there is some language design desire to verify 
that a class exists so that you have not done a typo, It goes against 
the grain of a runtime language that loads only the components (eg. 
classes/extensions) as it needs them.

How about:
try  { } catch ("ClassName" $e) { ..... }
if ($e instanceof "ClassName") {.....}

This would allow you to do only explicit classname testing rather than 
class exists and type testing

Regards
Alan






>regrads
>marcus
>
>Wednesday, September 1, 2004, 3:04:03 PM, you wrote:
>
>  
>
> > […]
>
>  
>
> > […]
>
>  
>
> > […]
>        
>  
>
> > […]
>
>  
>
> > […]
>
>  
>
> > […]
>
>
>  
>
> > […]
>
>  
>
> > […]

---

## #12541 — Derick Rethans — 2004-09-02T06:47:44+00:00

URL: https://externals.io/message/12523#12541 · In reply to: #12538

> Marcus Boerger wrote:
>
> > […]
> dont you think this is a really horrible kludge?

But so are exceptions in the first place ;-)

Derick
