Connecting Tech Pros Worldwide Forums | Help | Site Map

Errors to Exceptions

Cyphos
Guest
 
Posts: n/a
#1: Nov 3 '08
Hello,

I'm late getting to the PHP scene, but I thought I'd start to get into
it. I have a .NET and Ruby background, therefore I'm an OOP guy.

One thing that I haven't figured out to do is turn on Exception
handling for PHP errors. I'm used to exceptions, and noticed that PHP
5 has support for exceptions, so I'd like all errors thrown as
exceptions.

If anyone could help me with that, I'd very much appreciate it.

Thanks,
Mike

Jerry Stuckle
Guest
 
Posts: n/a
#2: Nov 3 '08

re: Errors to Exceptions


Cyphos wrote:
Quote:
Hello,
>
I'm late getting to the PHP scene, but I thought I'd start to get into
it. I have a .NET and Ruby background, therefore I'm an OOP guy.
>
One thing that I haven't figured out to do is turn on Exception
handling for PHP errors. I'm used to exceptions, and noticed that PHP
5 has support for exceptions, so I'd like all errors thrown as
exceptions.
>
If anyone could help me with that, I'd very much appreciate it.
>
Thanks,
Mike
>
It depends on what kind of errors you're talking about. Some cannot be
handled at all.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

=?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=
Guest
 
Posts: n/a
#3: Nov 3 '08

re: Errors to Exceptions


Cyphos escribió:
Quote:
I'm late getting to the PHP scene, but I thought I'd start to get into
it. I have a .NET and Ruby background, therefore I'm an OOP guy.
>
One thing that I haven't figured out to do is turn on Exception
handling for PHP errors. I'm used to exceptions, and noticed that PHP
5 has support for exceptions, so I'd like all errors thrown as
exceptions.
I've never tried myself but you can set your own error handler and use a
custom function that throws exceptions:

http://es.php.net/manual/en/language...ions.php#86575

Just be aware that some error types cannot be handled at all.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Gordon
Guest
 
Posts: n/a
#4: Nov 3 '08

re: Errors to Exceptions


On Nov 3, 1:34*pm, Cyphos <mweich...@gmail.comwrote:
Quote:
Hello,
>
I'm late getting to the PHP scene, but I thought I'd start to get into
it. I have a .NET and Ruby background, therefore I'm an OOP guy.
>
One thing that I haven't figured out to do is turn on Exception
handling for PHP errors. I'm used to exceptions, and noticed that PHP
5 has support for exceptions, so I'd like all errors thrown as
exceptions.
>
If anyone could help me with that, I'd very much appreciate it.
>
Thanks,
Mike
Exceptions are a relatively new addition to PHP, they didn't exist as
a concept in PHP 4. PHP 4 would instead emit an error message and
continue or abort depending on the error's severity. For backwards
compatibility reasons, any function that existed in PHP 4 still
behaves like this in PHP 5.

You can write wrapper functions yourself for whatever you need to
throw an exception. For example:

function myFopen ($path, $mode, $use_include_path = false, $context =
NULL)
{
if ($handle = fopen ($path, $mode, $use_include_path, $context))
{
return ($handle);
}
else
{
throw new Exception ('Unable to open file ' . $path);
}
}

You can then use your own function where you would use fOpen inside a
try block and put whatever error recovery code you feel you need in
the catch block.

The fopen call inside the function will of course still cause an error
message to be emitted. You can get around this by either turning
error reporting off or by using the @ operator to suppress the error.
Both have their benefits and drawbacks. Turning off error reporting
has no overhead and in a production environment is recommended anyway
because it prevents information that might be useful to hackers being
emitted by malfunctioning scripts. However in a test environment it
turns off all error reporting and hinders debugging as a result. The
@ operator can be used on a per call basis, but it has considerable
overhead and can really slow a PHP script down if used incautiously
(in a loop for example).
Rzzap
Guest
 
Posts: n/a
#5: Nov 3 '08

re: Errors to Exceptions


Álvaro G. Vicario wrote:
Quote:
Cyphos escribió:
Quote:
>I'm late getting to the PHP scene, but I thought I'd start to get into
>it. I have a .NET and Ruby background, therefore I'm an OOP guy.
>>
>One thing that I haven't figured out to do is turn on Exception
>handling for PHP errors. I'm used to exceptions, and noticed that PHP
>5 has support for exceptions, so I'd like all errors thrown as
>exceptions.
>
I've never tried myself but you can set your own error handler and use a
custom function that throws exceptions:
>
http://es.php.net/manual/en/language...ions.php#86575
This is indeed along the lines of what I do, keep in mind to check
wether the specific error type is in error_reporting :)

function my_error_handler($errno, $errstr, $errfile, $errline){
if($errno & error_reporting()){
throw new LogException($errstr);
}
return true;
}

Although, when one develops you could want to see the errors generated
(strict ones etc.), in which case you could alter the function to also
display it and/or log it somewhere.
Quote:
Just be aware that some error types cannot be handled at all.
Indeed.

Grtz,

Rik
Closed Thread