PHP
  Home arrow PHP arrow Page 4 - Intercepting Customized 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

Intercepting Customized Exceptions in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-10-22


    Table of Contents:
  • Intercepting Customized Exceptions in PHP 5
  • Triggering customized exceptions in PHP 5
  • Deriving a subclass from the built-in Exception class
  • Intercepting some MySQL-related 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


    Intercepting Customized Exceptions in PHP 5 - Intercepting some MySQL-related exceptions
    ( Page 4 of 4 )

    In the prior section, you hopefully learned how to derive a basic subclass from the built-in "Exception" class bundled with PHP 5 to create a customized exception mechanism that permits you to intercept all of the exceptions triggered by the sample "MySQL" class defined previously.

    So now it's time to set up an example to demonstrate the real functionality of the exception mechanism. Please study the following code sample, which shows how to handle generic and MySQL-related exceptions by using a couple of "catch()" blocks. Here it is:

    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 a MySQL exception)

     

    /* displays the following

    Catching MySQL exceptions...

    Exception message: Error connecting to the server

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

    Source line of exception: 36

    */

    }

    // catch MySQL exceptions here

    catch(MySQLException $e){

    echo $e->showExceptionInfo();

    exit();

    }

    // catch default exceptions here

    catch(Exception $e){

    echo 'Catching default exceptions...<br />';

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

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

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

    exit();

    }


    As shown by the above hands-on example, each time the pertinent "MySQL" abstraction class throws an exception of type "MySQLException," it will be neatly caught by the first "catch()" block and processed in a pretty trivial manner. On the other hand, all of the generic exceptions will be intercepted by the second block, in this way demonstrating how simple it is to develop a customized exception mechanism with PHP 5 by means of simple inheritance. Simple and nice, right?

    Finally, I recommend that you play with all of the code samples included in this tutorial, so you can grasp more quickly how to take advantage of using exception subclasses in PHP 5-controlled applications.

    Final thoughts

    In this second chapter of the series, you hopefully learned how to combine a MySQL abstraction class along with a subclass derived from the built-in "Exception" parent, in order to implement a customized exception system with PHP 5. As you saw previously, this process is actually a no-brainer and can be mastered in a very short time.

    You should notice, however, that all of the tasks aimed at processing MySQL result sets were handled specifically by a "Result" class. Bearing in mind this concept, in the next tutorial I'm going to teach you how to modify the signature of this particular class to provide it with the capacity for throwing data set-related exceptions.

    Want to learn how this will be done? Don't miss the next part!



     
     
    >>> 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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek