Beruflich Dokumente
Kultur Dokumente
Notice: Undefined variable: y in /home/eddo/workspaces/neon/ZendCon08
ServerIndie/xxx.php on line 6
Warning: fopen(thisisnotarealfile) [function.fopen]: failed to open
stream: No such file or directory in
/home/eddo/workspaces/neon/ZendCon08ServerIndie/xxx.php on line 8
Fatal error: Call to undefined function nonexistingfunction() in
/home/eddo/workspaces/neon/ZendCon08ServerIndie/xxx.php on line 10
function getFooPlusOne($foo) {
if (3 > $foo) {
trigger_error('foo has to be greater than 3', E_USER_ERROR);
}
return ($foo + 1);
}
• in php.ini file
error_reporting = E_ALL & ~E_NOTICE
• in runtime
error_reporting(E_ALL & ~E_NOTICE);
• in .htaccess or apache.conf
php_value error_reporting 6135
• Display them
• Log them
• Ignore them
• Act on them
Don't do that.
set_error_handler("demoErrorHandler");
$db = mysql_connect();
mysql_query('SELECT * FROM users WHERE id=18');
$db = mysql_connect();
if (! $db) {
redirectToErrorPage('Could not connect to the database!');
die(1);
}
mysql_query('SELECT * FROM users WHERE id=18', $db);
class DataBaseException extends Exception {
class MathException extends Exception {
Terminology:
• throw – the act of publishing an Exception
• try block – a segment of the code which may have an
exception thrown in it
• catch block – a segment of the code which handles an
exception if one happens
• finally – is not available in PHP, but common in other
languages
try {
if (0 == $denominator) {
throw new Exception('Zero denominator');
}
echo ($numerator / $denominator);
} catch (Exception $e) {
echo 'You can not divide by zero';
die; // make sure the script stops
/**
* @param float $numerator
* @param float $denominator
* @return float
* @throws MathException
*/
function divide($numerator, $denominator) {
if (0 == $denominator) {
throw new MathException('Zero denominator');
}
return ($numerator / $denominator);
}
try {
$calc = new Calculator();
echo $calc>divide($numerator, $denominator);
} catch (MathException $e) {
echo 'A mathematic integrity failure: ', $e>getMessage();
} catch (Exception $e) {
echo 'A system error: ', $e>getMessage()
}
echo 'Done';
try {
$user = new User($username);
$user>authenticate($password);
$user>getAccountBalance();
} catch (UserAuthenticationException $e) {
echo "The user is not logged in";
} catch (DataBaseException $e) {
Logger::logException('DB Error', $e);
echo "The system has encounter some internal error";
} catch (Exception $e) {
Logger::logException('Unknown error', $e);
echo "The system has encounter some internal error";
}
private $comment = 'Zend Conference 2008 Example';
private $backtrace;
public function __construct() {
$this>code = mysql_errno();
$this>message = mysql_error();
$this>backtrace = debug_backtrace();
}
public function __toString() {
return 'Papa was a Rolling Stone';
}
}
try {
if (! mysql_connect()) {
throw new MysqlException();
}
} catch (MysqlException $e) {
echo $e->getMessage();
} catch (Exception $e) {
// do something else
}
try {
$user = User::fetch('Eddo', 'karnaf');
} catch (Exception $e) {
// redirect to an error page
die;
}
private $name;
private $data;
public function __construct($name) {
$this>name = (string)$name;
$this>data = UserModel::getDataByName($name);
if (empty($this>data)) {
throw new Exception("The system failed for {$name}");
}
}
}
try {
$user = new User('Eddo Rotman');
} catch (Exception $e) {
throw new Exception('Could not find the user');
}
function myDefaultExceptionHandler($exception) {
// do something about it
}
set_exception_handler('myDefaultExceptionHandler');
class MyExcpetionHandling {
public static function doSomething($exception) {
// do something about it
}
}
set_exception_handler(array('MyExcpetionHandling', 'doSomething'));