Tag Archives: catch

Quick Reflexes! Sport & Health 26 JUL 2012

Evan Michael Longoria (born October 7, 1985) is a Major League Baseball third baseman for the Tampa Bay Rays. Formerly, Longoria was a star infielder for the Long Beach State University baseball team, the 2005 Cape Cod League MVP, and the 2006 Big West Co-Player of the Year.

And thanks to this display of amazingly quick relexes combined with what must have been a pretty damn sore bare-handed catch of a baseball about to hit a television reporter, he might just become as well known as his namesake Eva Longoria.

Though probably not.

Still, awesome display of reflexes!

PHP: Simple Try Catch Example CodeUnit 16 JAN 2010

With object orientated languages came the concept of the try-catch block to better manage exceptions and application crashes, and from PHP 5 onwards, the popular web server-side scripting language too adopted this ideology.

By placing code which stands a possible chance of failing within a try block, you are alerting PHP to the fact that should it fail to correctly process that chunk of code, it needs to pass control over to the code sitting within the catch block definition and then continue running after that backup code has been executed.

To see this in action, consider the code below:

try {
    $error = 'Throw this error';
    throw new Exception($error);
    echo 'Never get here';
catch (Exception $e)
    echo 'Exception caught: ' . $e->getMessage() . "\n";

Now admittedly in the above code block we are cheating a little by forcing the script to ‘crash’ by throwing an exception, but in the usual case we’d have pretty normal code in there like opening a file perhaps. Should it execute correctly, the application would simply continue to run, but had it failed, like we’re forcing it to do in the example above, PHP passes control over the exception handler code sitting in the catch code block, this time performing a simple echo out of the trapped error message.

So in other words, a simple but powerful tool that any current era developer should be making use of in order to better ensure the stability of their scripts.