PHP
  Home arrow PHP arrow Page 3 - Error Handling in PHP: Introducing Exceptions in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Error Handling in PHP: Introducing Exceptions in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 39
    2006-01-18


    Table of Contents:
  • Error Handling in PHP: Introducing Exceptions in PHP 5
  • The basics of exceptions: using the “throw” statement and “try-catch” blocks
  • Obtaining detailed error information: using built-in Exception class’ additional methods
  • Working with error types: developing an improved error handling mechanism
  • Exception subclassing: extending the functionality of the built-in Exception class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Error Handling in PHP: Introducing Exceptions in PHP 5 - Obtaining detailed error information: using built-in Exception class’ additional methods
    ( Page 3 of 5 )

    As I mentioned earlier, it’s possible to get detailed information about the error that occurred. It’s precisely for this reason that the Exception class provides developers with some other methods, which can offer additional details about the failure and its environment. Here is the sample “FileReader” class, which now uses these complementary methods:

    class FileReader{
      private $file;
      private $fileDir='fileDir/';
      public function __construct($file){
        if(!file_exists("{$this->fileDir}{$file}.php")){
          throw new Exception('File '.$file.' not found');
        }
        $this->file=$file;
      }
      public function getContent(){
        if(!$content=file_get_contents("{$this->fileDir}{$this-
    >file}.php")){
          throw new Exception('Unable to read file contents');
        }
        return $content;
      }
    }

    // instantiate FileReader object and implement try-catch
    statement providing more information on the error
    try{
      $fr=new FileReader('sample_file'); // potential error condition
      // echo file content
      echo $fr->getContent(); // potential error condition
    }
    catch(Exception $e){
      echo 'Error :'.$e->getMessage().'<br />';
      echo 'Code :'.$e->getCode().'<br />';
      echo 'File :'.$e->getFile().'<br />';
      echo 'Line :'.$e->getLine().'<br />';
      exit();
    }

    In addition to the methods illustrated above, the Exception class exposes the “getTrace()” and “getTraceAsString()” methods. The first one returns an array providing information on the progress of an exception, while the second one shows the same data, but in string format.

    Returning to the example above, it’s nearly the same as the previous one. However, the “catch” block uses the additional methods of the exception object, helpful for obtaining more specific details on the error that occurred. With reference to this, the set of “echo” statements displays the error message passed in when the exception was originally thrown, as well as the error code (if any), the file containing the error, and finally the line at which the failure happened.

    Now, the benefits of exceptions should be pretty clear. Notice how easy it is to set up an error management mechanism that can be developed through specific code, while maintaining program flow completely free from dealing with potential failures. In addition, as you’ve seen in the above example, a greater level of detail on the error that occurred can be easily obtained by calling up some of the complementary methods.

    What else can we ask for? Well, sometimes it’s highly advantageous to have an error handling system that is capable of determining what course of action to take, according to the type of error generated. While this may sound like common sense stuff, in practical terms, it isn’t so intuitive to code. Thus, let’s address this issue and set up an example, which takes different actions according to the type of exception generated.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT