 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
Ole Markus' Blog: Catching fatal errors in PHP
by Chris Cornutt March 11, 2011 @ 09:16:42
Ole Markus has a new post today looking at how you can catch fatal errors in your PHP applications a bit more gracefully than the usual failure messages.
In dynamic languages like PHP [errors like E_ERROR and E_PARSE] happen all the time, for example when trying to call a method on a variable you assumed was an instance of a specific class, but which for some reason suddenly was not instantiated. Not only are they often not catched, but often it is also difficult to even know that they are occurring.
His solution comes in the form of a built-in PHP function, register_shutdown_function, that executes when the PHP process is shutting down - errors or not. It takes in a callback method that has access to an exception object. You can get lots of interesting information from this object and, as in his example, log it to a file for future investigation.
voice your opinion now!
catch fatal error registershutdownfunction exception zendlog
PHP Developer Blog: Unit Tests How to test for Exceptions
by Chris Cornutt April 20, 2009 @ 12:06:15
The PHP Developer Blog has a quick post for the unit testers out there on how to work with exception handling in your tests.
When unit testing, you'd also want to test whether your application throws Exceptions as expected (the following examples are based on SimpleTest). Assumption for the examples is, that we have a method that expects an integer as parameter.
Putting the assertion inside of the catch block won't work correctly since it wouldn't happen unless an exception is thrown. Instead he recommends putting it right after the exception try/catch and check to see if the exception variable is a type of 'Exception' (with another potential solution of adding in a check for an 'InvalidArgumentException').
voice your opinion now!
invalidargumentexception assert catch try exception unittest test
Quinton Parker's Blog: Try-catch suppress?
by Chris Cornutt March 20, 2009 @ 07:56:13
In this new entry to his blog Quinton Parker looks at some strangeness he's found around the try/catch functionality in PHP. His specific example involves file_get_contents.
PHP never ceases to amaze me. Just the other day a colleague discovered that you can suppress error messages reported by file_get_contents() using the try-catch statement. That should've raised an eyebrow.
His sample code shows the normal error that a file_get_contents on a nonexistent file would give then wraps it in a try/catch. The same path is put into the file_get_contents but, because of some sort of interesting handling, isn't reported in the catch. He's at a loss and is asking for help figuring this one out from the readers out there. Be sure to leave a comment if you have more info.
voice your opinion now!
try catch exception handling supress filegetcontents
DevShed: Sub Classing Exceptions in PHP 5
by Chris Cornutt October 15, 2008 @ 12:06:24
DevShed has start up a new series today with the first part in a four-part series looking at exception handling in PHP5.
If you do any serious programming, whether it's in PHP 5 or some other language, you've needed to know how to handle run time errors and other "exceptional" conditions. You can do this by making your program throw generic exceptions. Or you can unlock the potential of PHP 5 and learn how to create custom exceptions, which is the subject of this four-part series.
In this first part they get you started with exceptions, showing how to throw them and catch them correctly (try/catch). They put it to good use in an example catching exceptions thrown from a MySQL connection and select.
voice your opinion now!
php5 exception tutorial subclass try catch
Michelangelo van Dam's Blog: Throwing and catching exceptions
by Chris Cornutt October 14, 2008 @ 14:32:59
Michelangelo van Dam has written up a quick introductory post on his blog about the process around throwing and catching exceptions in your apps.
One thing that I noticed was that although the code was well written [in the Zend Framework], implementing coding standards and best practices on many of the classes, I did notice a wrong usage of throwing exceptions (the try - catch statements).
He gives a few examples - catching a "divide by zero" the right and wrong way, how to grab/handle the message that comes along with the exception and how to define your own custom exception handler to help your code do more useful things with the errors it might throw.
voice your opinion now!
throw catch exception introduction custom handler
Stefan Priebsch's Blog: Turning errors into exceptions
by Chris Cornutt April 30, 2008 @ 12:53:35
In a recent blog entry Stefan Priebsch shows how to take an error thrown by your script and turn it into an exception (to make things like catchable fatal errors).
While I would personally prefer an exception to be thrown in the first place, it is pretty easy to convert errors to exceptions in PHP.
His example is pretty simple - you set a custom error handler in your script that pulls in the error information and tosses an exception based on the error number the handler is given. Then you can use the try/catch method to see if your script has tossed an exception of the fatal error type. Nice simple solution to handle an interesting little problem.
voice your opinion now!
error exception convert try catch fatal errorhandler
PHP-Coding-Practices.com: Try-Catch Syntax Weirdness
by Chris Cornutt June 22, 2007 @ 13:28:00
In working with his code recently, Tim Koschuetzki noticed something odd with a block of try/catch code:
I just noticed today, that PHP's try catch blocks require curly braces. Anybody has an idea why it is like that? I have used curly braces by default up until now, so I just stumbled upon this weirdness today.
He includes two examples, one with a curly brace after the catch clause and the other without. This is different than several other control structures (like ifs) that don't require the curly brace when there's only the one line following it.
voice your opinion now!
try catch curlybrace require try catch curlybrace require
|
Community Events
Don't see your event here? Let us know!
|