Home arrow PHP arrow Page 2 - Error Handling for Dynamic Twitter Signature Images with PHP

Understanding error-handling - PHP

Over the course of my past two articles Iíve been showing you how to build a script capable of creating and displaying a dynamic signature image containing the latest status from a userís Twitter feed. In the third installment in this series, I will be demonstrating how to add proper object-oriented error handling to the SignatureImage class.

TABLE OF CONTENTS:
  1. Error Handling for Dynamic Twitter Signature Images with PHP
  2. Understanding error-handling
  3. Throwing exceptions for errors
  4. Playing throw and catch
By: Nilpo
Rating: starstarstarstarstar / 1
August 25, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before we dive too far into todayís new code, itís important that you understand the error-handling mechanisms available to you in PHP 5.  To do that, youíll need to familiarize yourself with PHPís Exception class.

The Exception class provided by PHP is used to throw Exceptions in your application whenever errors occur.  Those exceptions may then be caught and handled in a variety of ways according to your applicationís needs.  Letís examine a very simple example of the use of a PHP exception.

$x = 0;

try {

    if $x == 0 {

        throw new Exception('Cannot divide by 0');

    }

    $y = 100 / $x;

}

catch (Exception $e) {

    echo $e->getMessage();

}

This examines attempts to divide the 100 by some number in the variable $x.  Iíve created an error state by setting $x equal to 0, since you cannot divide any number by 0.  To prevent PHP from displaying an error for this division, Iíve surrounded the process in an If statement that first determines whether or not $x contains the value 0.  To this point, this should all be fairly standard procedure since this is a classic example of error prevention.

Next, Iíve employed the use of exceptions.  Whenever $x equals 0 this code will throw an exception with the message ďCannot divide by 0.Ē  This is done using the throw statement.  Notice the use of the ďnewĒ keyword.  We want to throw a new instance of the Exception class while passing it a string that contains the error message.

This is sufficient to throw an Exception in PHP.  However, we canít actually do anything with that exception yet.  For this to be effective, the code needs to be contained in a Try block.  This tells PHP to be on the lookout for possible exceptions.

Whenever PHP encounters an exception within a Try block, it will halt execution of the Try block and look for the first Catch block to catch and handle the exception.  In this case, the catch block simply writes the error message back.  This is a very simple demonstration.  Youíll see the true power of exceptions as you see them used in our application.



 
 
>>> More PHP Articles          >>> More By Nilpo
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: