Errors to Exceptions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cyphos

    Errors to Exceptions

    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

    #2
    Re: Errors to Exceptions

    Cyphos wrote:
    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@attgl obal.net
    =============== ===

    Comment

    • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

      #3
      Re: Errors to Exceptions

      Cyphos escribió:
      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:



      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
      --

      Comment

      • Gordon

        #4
        Re: Errors to Exceptions

        On Nov 3, 1:34 pm, Cyphos <mweich...@gmai l.comwrote:
        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_pa th = false, $context =
        NULL)
        {
        if ($handle = fopen ($path, $mode, $use_include_pa th, $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).

        Comment

        • Rzzap

          #5
          Re: Errors to Exceptions

          Álvaro G. Vicario wrote:
          Cyphos escribió:
          >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_handle r($errno, $errstr, $errfile, $errline){
          if($errno & error_reporting ()){
          throw new LogException($e rrstr);
          }
          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.
          Just be aware that some error types cannot be handled at all.
          Indeed.

          Grtz,

          Rik

          Comment

          Working...