Back to the blog
Recent Posts
-
May 18
A short story about usability
-
Apr 20
Twitter as social computer
-
Apr 03
techAU troubles - techAU.tv complains, new domain is born
-
Mar 30
Announcing the launch of techAU.com.au
-
Mar 27
TechAU repurposed as Aussie tech blogger aggregator
-
Mar 20
Are aggregator sites getting a free ride?
Most Popular Posts
-
Why you should be using a framework
-
Five easy things that make you a better web developer
-
Internet Expletive
-
Where's the Android hype?
About the Blog

I'm a web designer and web application developer in Melbourne, Australia. If you find anything useful, leave me a comment, and if you need web design, development, or accessibility and usability consulting, contact me! Cheers.
Twitter: joshsharp
Turning PHP errors into Exceptions
Sunday 11 Nov, 2007 08:04 PM
I've just been browsing the PHP6 meeting minutes to see if anything exciting is coming up in the next release. There are a couple of handy things which I might post about soon, but in the meantime this useful code snippet caught my eye. I wasn't aware you could handle errors yourself in PHP5, but it seems it is possible to do so. The following catches all errors of level E_NOTICE and throws an exception instead. Pretty handy if you already use Exceptions for your own errors — now you can decide what to do with parser errors, instead of being forced into displaying them or just ignoring them.
The code:
<?php
function error_handler($errorType, $message){
if ($errorType == E_NOTICE) {
throw new Exception( $message, $errorType);
}
}
set_error_handler('error_handler');
?>
