PHP 5 Exception & Error Handling

Note PHP try and catch does not catch errors unless you convert errors to exceptions first !
http://uk.php.net/manual/en/class.errorexception.php

example
function exception_error_handler($errno, $errstr, $errfile, $errline, $errcontext ) {
	throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");

function c() {
 $i=1/0;
}

function b() {
 c();
}

function a() {
 b();
}

try {
 a();
}
catch(ErrorException $e) {
 print 'x'.PHP_EOL;
 var_export($e);
}
catch(Exception $e) {
 print 'y'.PHP_EOL;
 var_export($e);
}


x
ErrorException::__set_state(array(
   'message' => 'Division by zero',
   'string' => '',
   'code' => 0,
   'file' => '/Users/clive/Documents/workspace/q.php',
   'line' => 8,
   'trace' => 
  array (
	0 => 
	array (
	  'file' => '/Users/clive/Documents/workspace/q.php',
	  'line' => 8,
	  'function' => 'exception_error_handler',
	  'args' => 
	  array (
	  ),
	),
	1 => 
	array (
	  'file' => '/Users/clive/Documents/workspace/q.php',
	  'line' => 12,
	  'function' => 'c',
	  'args' => 
	  array (
	  ),
	),
	2 => 
	array (
	  'file' => '/Users/clive/Documents/workspace/q.php',
	  'line' => 16,
	  'function' => 'b',
	  'args' => 
	  array (
	  ),
	),
	3 => 
	array (
	  'file' => '/Users/clive/Documents/workspace/q.php',
	  'line' => 20,
	  'function' => 'a',
	),
  ),
   'severity' => 2,
))


formatted
...
catch(ErrorException $e) {
 print $e->getMessage();
 print $e->getFile();
 print $e->getLine();
 print PHP_EOL;

 print $e->getTraceAsString();
 print PHP_EOL;
}
...


REFERRERS
PhpErrorHandling
PhpExceptionHandling
PhpFunctions
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki