How to handle those pesky errors in PHP
(Page 3 out of 4)PHP5 and Exceptions
PHP5 has got a new type of errors called exceptions, something you might already be familiar with if you've used another programming language (like Java). Exceptions are mainly useful if you want to trigger your own errors, and most standard functions in PHP5 still throw regular errors (which is a shame really). The below code demonstrates a simple exception:
try {
$error = 'Always throw this error';
throw new Exception($error);
// Code following an exception is not executed.
echo 'Never executed';
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
echo 'And the code execution goes on...';
?>
The try block is used to do a certain action whereby an exception might be thrown, which can then be handled by the catch block, as seen in the above example. The example is pretty pointless though, as there will always be an exception in the try block since an exception is actually thrown there on purpose. But imagine something like this:
try {
// Try loading file
$data = load_file('C:\does\not\exist.txt');
// Code following an exception is not executed.
echo 'Never executed';
} catch (Exception $e) {
// Can't load the file, handle it
echo 'Unable the load file because: ' . $e->getMessage() . "\n";
}
echo 'And the code execution goes on...';
function load_file ($file) {
if (file_exists($file) == false) {
throw new Exception('File doesn\'t exist');
return false;
}
// load the file here
}
?>
The above example really demonstrates why exceptions can be so useful. By throwing an exception in the load_file function, you can handle the problem in the catch block, making it much easier to handle errors in PHP5 than in PHP4.
The only downside of using exceptions is that you must always catch them, or otherwise they are printed to the visitor, and they look even worse then regular errors. But it's also possible to set an exception handler to handle all the uncaught exceptions, like so:
function handle_exceptions($e) {
echo "Uncaught exception: " , $e->getMessage(), "\n";
}
set_exception_handler('handle_exceptions');
// This exception won't be caught
// since it's not in a try-catch block
$error = 'Always throw this error';
throw new Exception($error);
echo 'Never executed';
?>
In the example above the exception isn't in a try-catch block, so it won't get caught, but the exception handler will take care of it. Be aware though that once the exception handler is called, execution of the script stops, so it's always better to use a try-catch block.
You can also modify the exception handler to write to the log file as well, e.g.
// Location of the error log file
define ('logfile', dirname(__FILE__) . '/logfile.txt');
function handle_exceptions($e) {
$error = '[' . date('d/m/Y H:i:s') . '] EXCEPTION: ';
$error .= $e->getMessage() . " in line " . $e->getLine() . " of file " . $e->getFile() . "\n";
// Log error
$f = fopen(logfile, 'a');
fwrite($f, $error);
fclose($f);
echo 'Sorry, something unexpected happen, and it has been logged for further investigation. Please go back where you came from. Thank you for your understanding.';
}
set_exception_handler('handle_exceptions');
// This exception won't be caught
// since it's not in a try-catch block
$error = 'Always throw this error';
throw new Exception($error);
echo 'Never executed';
?>
That's about it for exceptions. Let's move on to the final part of the article: how to trigger your own errors.
April 11th, 2006 at 8:08 am
help
can anyone teach me how to disable the refresh button because that is the cause of my errors
hehhehehe
thanks Master Programmers
July 26th, 2006 at 4:40 pm
Hi I would like to use your error code but i am having problems with defining the directory the line from the tutorial is: define (’logfile’, dirname(__FILE__) . ‘/logfile.txt’);
I have changed this to: define (’logfile’, C:/log/logfile.txt(__FILE__) . ‘/logfile.txt’);
It throws up a parse error, I am a newbie so any help would be appreciated.