Catching all errors

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

Threaded View
I've noticed that some exceptions aren't being caught by catch
(Exception $e).

The following script gives me a php error, how do I ensure that any
errors will be caught by my own handler?



      catch(Exception $e)
          echo "error caught";


Re: Catching all errors

Mark Smith wrote:
Quoted text here. Click to load it

Very few PHP errors can be caught by exceptions, and those which can are
typically in the newer class libraries.  You can use set_error_handler()
to set a function to handle most non-syntactical errors.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Catching all errors

Mark Smith escribió:
Quoted text here. Click to load it

Well, errors and exceptions are not exactly the same. According to some
user notes I've read in the manual, you can create a custom error
handler that throws exceptions and then catch these exceptions. I've
never tried but it looks promising:

Fatal errors and the like, however, will always remain uncatchable.

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor al baño María:

Re: Catching all errors

Quoted text here. Click to load it

Exceptions are a reliability new concept in PHP programming.  None of
the functions that existed in the language prior to PHP 5 were written
with exceptions in mind and few (none?) of them have been modified to
throw an exception when they fail.

You could put wrappers around functions you want to throw exceptions,
but this is quite laborious.

function fileOpen ($filename, $mode, $use_include_path=3Dfalse,
    if $handle = fopen ($filename, $mode, $use_include_path,
        return ($handle);
        throe new Exception ('Failed to open file ' . $filename);

Site Timeline