PHP
  Home arrow PHP arrow Page 3 - Error Handling in PHP: Introducing Exc...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 4 stars4 stars4 stars4 stars4 stars / 31
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · This last installment of the series explores specifically error handling in PHP 5....
       · thank you very much for your nice article, especially for the exception part.just...
       · Thank you so much for your comments about my PHP exceptions-related tutorial.If you...
       · What I mean is that:catch(Exception $e){ // if error occurred when reading...
       · Thank you again for your comments. Now that you clarified your point, it's clear to...
       · Nice Description. Thanks a lot for explaining
       · Thank you for the kind comments on my PHP article, and I'm glad to know it was...
       · Hey Alejandro, I just wanted to thank you so much for writing this article as well...
       · Thank you for the kind words on my PHP article. Simply I didn’t use constants in...
       · Hi Haret,Thank you for the compliments on my PHP article. I really appreciate...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT