How to catch a failed opening required ... (require_once) ?

How to catch a failed opening required ... (require_once) ?

am 09.08.2006 17:36:15 von reply

Hi,
How can you catch a fatal error that occurs when you use a
require_once("myfile.php") and the file dioesn't exist ?
I tried:
try {
require_once("p.php");
} catch (Exception $e) {
echo $e->getMessage();
}
but it returns
Warning: require_once(p.php) [function.require-once]: failed to open stream:
No such file or directory in D:\apache\xampp\htdocs\site\untitled.php on
line 12

Fatal error: require_once() [function.require]: Failed opening required
'p.php' (include_path='.;D:\apache\xampp\php\pear\') in
D:\apache\xampp\htdocs\site\untitled.php on line 12

So the try/catch is in fact completely ignored.

Pugi!

Re: How to catch a failed opening required ... (require_once) ?

am 09.08.2006 18:24:33 von pritaeas

"Pugi!" wrote in message
news:lvmdnZfd_o9vnUfZRVnyrQ@scarlet.biz...
> Hi,
> How can you catch a fatal error that occurs when you use a
> require_once("myfile.php") and the file dioesn't exist ?
> I tried:
> try {
> require_once("p.php");
> } catch (Exception $e) {
> echo $e->getMessage();
> }
> but it returns
> Warning: require_once(p.php) [function.require-once]: failed to open
stream:
> No such file or directory in D:\apache\xampp\htdocs\site\untitled.php on
> line 12
>
> Fatal error: require_once() [function.require]: Failed opening required
> 'p.php' (include_path='.;D:\apache\xampp\php\pear\') in
> D:\apache\xampp\htdocs\site\untitled.php on line 12
>
> So the try/catch is in fact completely ignored.
>
> Pugi!
>
>

You can replace it with:

if (file_exists ("p.php")) require_once ("p.php");
?>

Re: How to catch a failed opening required ... (require_once) ?

am 09.08.2006 19:48:36 von Gleep

On Wed, 9 Aug 2006 17:36:15 +0200, "Pugi!" wrote:

>Hi,
>How can you catch a fatal error that occurs when you use a
>require_once("myfile.php") and the file dioesn't exist ?
>I tried:
>try {
> require_once("p.php");
>} catch (Exception $e) {
> echo $e->getMessage();
>}
>but it returns
>Warning: require_once(p.php) [function.require-once]: failed to open stream:
>No such file or directory in D:\apache\xampp\htdocs\site\untitled.php on
>line 12
>
>Fatal error: require_once() [function.require]: Failed opening required
>'p.php' (include_path='.;D:\apache\xampp\php\pear\') in
>D:\apache\xampp\htdocs\site\untitled.php on line 12
>
>So the try/catch is in fact completely ignored.
>
>Pugi!
>



Switch statements to include_once then if there is an error at least it shows up and gives
you a clue how to fix.

Re: How to catch a failed opening required ... (require_once) ?

am 17.08.2006 17:36:26 von Don Freeman

"Hans 'pritaeas' Pollaerts" wrote in message
news:ebd2hp$ue$1@news5.zwoll1.ov.home.nl...
>
> "Pugi!" wrote in message
> news:lvmdnZfd_o9vnUfZRVnyrQ@scarlet.biz...
>> Hi,
>> How can you catch a fatal error that occurs when you use a
>> require_once("myfile.php") and the file dioesn't exist ?
>
> You can replace it with:
>
> > if (file_exists ("p.php")) require_once ("p.php");
> ?>
>

You would want to add an "else" clause as well (to display an error message
and then exit?), otherwise the need for the file to be required will not be
satisfied.

--
-Don
Ever had one of those days where you just felt like:
http://cosmoslair.com/BadDay.html ?
(Eating the elephant outside the box, one paradigm at a time)