PHP
  Home arrow PHP arrow Page 3 - Sub Classing 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? 
Google.com  
PHP

Sub Classing Exceptions in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-10-15


    Table of Contents:
  • Sub Classing Exceptions in PHP 5
  • Getting started with exceptions with PHP 5
  • Catching MySQL-related exceptions with the PHP 5 Exception class
  • Modifying the MySQL class's signature to trigger customized exceptions

  • 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


    Sub Classing Exceptions in PHP 5 - Catching MySQL-related exceptions with the PHP 5 Exception class
    ( Page 3 of 4 )

    In the previous section I showed you how to utilize the native exception mechanism provided by PHP 5 to handle some errors that might occur when connecting to MySQL, running queries against a selected database, etc. Of course, the simplest way to achieve this was by using a couple of SQL processing classes, whose respective signatures were shown previously.

    Nonetheless, since these concepts are pretty simple to grasp, let me go one step further and show you how the aforementioned classes are capable of triggering generic exceptions that can be caught within a typical “try-catch” block.

    Having said that, please take a look at the following code sample. It attempts to pull out some records from a sample “USERS” MySQL database table, while this server is turned off:


    try{

    // connect to MySQL

    $db=new MySQL(array('host'=>'host','user'=>'user','password'=>'password','database'=>'database'));

    // fetch data on some users

    $result=$db->query('SELECT * FROM users');

    // display data on some users

    while($row=$result->fetchRow()){

    echo 'First Name: '.$row['firstname'].' Last Name: '.$row['lastname'].' Email: '.$row['email'].'<br />';

    }

     

    // turn off MySQL (throws an exception)

     

    /* displays the following


    Exception message: Error connecting to the server

    Source filename of exception: path/to/file/exception_test.php

    Source line of exception: 26

    */

    }

    // catch all generic exceptions here

    catch(Exception $e){

    echo 'Exception message: '.$e->getMessage().'<br />';

    echo 'Source filename of exception: '.$e->getFile().'<br />';

    echo 'Source line of exception: '.$e->getLine();

    exit();

    }


    As you can see, the above hands-on example demonstrates in a nutshell how to use a simple “try-catch” block to intercept any generic exceptions triggered by the pertinent “MySQL” and “Result” classes defined earlier. The situation is in this case particularly illustrative, since the database server has been purposely turned off, which obviously triggers an exception that’s trapped by the corresponding “cacth()” block.

    So far, you have seen how to handle generic exceptions by using the base “Exception” class included with PHP 5. As I mentioned in the beginning of this article, however, it’s also possible to extend the functionality of this native class by creating some subclasses from it, and triggering customized exceptions that can be processed by multiple “try-catch()” blocks.

    Sounds interesting, doesn’t it? Therefore, in the following section I’m going to modify the respective signatures of the MySQL handling classes that you learned before to provide them with the capacity for triggering not only generic exceptions, but a couple of customized ones.

    To learn how this will be done, please click on the link below and keep reading.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek