Re: poll: howto do informative error handling without the fatalities

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Rene Veerman wrote:
> Hi,
> 
> I'm looking for a strategy to do informative error handling at all
> levels of my code, yet keep these errors non-fatal as often as
> possible.

error_log - for logging errors
throw Exception - for show stoppers
try/catch - for when you can handle a potential show stopper

custom error logging / messaging can easily be achieved with something
like this:

<?php

class Messenger
{

  private static $_messages = array();

  public static function addMessage( $string )
  {
    self::$_messages[] = $string;
    if( $string instanceof Exception ) {
      echo self::report();
    }
  }

  public static function report()
  {
    return implode( PHP_EOL , self::$_messages );
  }

}

set_exception_handler( 'Messenger::addMessage' );

Messenger::addMessage( 'little error report 1' );
Messenger::addMessage( 'little error report 2' );
Messenger::addMessage( 'little error report 3' );
throw new Exception( 'this will stop the script' );
// exception will kill the script; if you comment it out
// or wrap it in a try catch then you can keep going..
Messenger::addMessage( 'little error report 4' );
// try catch exceptions and report them like this..
try {
  throw new Exception( 'we could catch this' );
} catch ( Exception $e ) {
  Messenger::addMessage( $e );
}
Messenger::addMessage( 'little error report 5' );
// and when your done just echo it out or save it or..
echo Messenger::report();


Regards!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux