Errors to Exceptions

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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

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


Re: Errors to Exceptions

Cyphos wrote:
Quoted text here. Click to load it

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.

Re: Errors to Exceptions

Cyphos escribi:
Quoted text here. Click to load it

I've never tried myself but you can set your own error handler and use a
custom function that throws exceptions:

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

-- - lvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programacin web:
-- Mi web de humor al bao Mara:

Re: Errors to Exceptions

lvaro G. Vicario wrote:
Quoted text here. Click to load it

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.

Quoted text here. Click to load it




Re: Errors to Exceptions

Quoted text here. Click to load it

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 =
    if ($handle = fopen ($path, $mode, $use_include_path, $context))
        return ($handle);
        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).

Site Timeline