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

Playing throw and catch - 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.

  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



As I mentioned previously, throw statements donít do much good in PHP without a catch statement.  Knowing where to place them is dependent upon your application.  Since this particular application produces a single output (a single Twitter signature image), itís convenient to catch all of the exceptions in a single location.  The class can then either produce an image or not.

The classís constructor houses the function calls for the entire image creation process.  This makes it a convenient spot to place my try and catch blocks.  Quite simply, we can wrap the entire contents of the constructor in a try block.  Since each of the exception throws throughout our code are called as the result of a function call in the constructor, we never have to worry about having an unhandled exception.  This approach also keeps the entire error-handling process completely contained within the class itself.  This is a great approach for larger applications that may contain a great number of different classes.

    public function __construct($name, $bg_image, $adir)


        try {


            $this->fetchAvatar($this->profile_image, $adir);



        catch (Exception $e) {




If at any point during execution, the method calls in the constructor encounter an exception, execution of the method will stop and code execution will jump to the catch block.  This results in a call to PHPís die function, which displays the error message provided by the throw statement that generated the exception before ending code execution.

In this article you learned how to implement PHP's exceptions as a means of error-handling for our Twitter Signature Image class.  But donít go away just yet.  In the next article in this series I will show you how to create your own custom exception class to further take control of how errors are handled in PHP by creating a replacement image whenever errors occur.  Until next time, keep coding!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: