Home arrow PHP arrow Page 4 - Handling MySQL Data Set Failures in PHP 5

Handling MySQL and result set exceptions with separated try-catch blocks - PHP

If you’re a PHP developer who wants to learn the basics of implementing customized exceptions in PHP 5, then look no further, because you’ve come to the right place. This is the third part of a four-part series entitled “Subclassing exceptions in PHP 5.” It teaches you how to extend the native exception mechanism bundled with PHP 5 by using inheritance, and complements the corresponding theory with copious, illustrative hands-on examples.

TABLE OF CONTENTS:
  1. Handling MySQL Data Set Failures in PHP 5
  2. Intercepting MySQL-related exceptions with PHP 5
  3. Triggering custom exceptions when processing MySQL data sets
  4. Handling MySQL and result set exceptions with separated try-catch blocks
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
October 29, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I explained in the previous section, it's necessary to derive a subclass from the built-in "Exception" class bundled with PHP 5 to separately handle all of the specific exceptions triggered by the "Result" class coded previously. Therefore, I've included the signature of this brand new exception subclass below. It is responsible only for handling some errors that might occur when processing MySQL result sets.

Here's how this particular class looks:


// create ResultException class

class ResultException extends Exception{

public function __construct($message,$code=0){

// call parent of Exception class

parent::__construct($message,$code);

}

public function showResultExceptionInfo(){

return 'Catching Result exceptions...<br />Exception message: '.$this->getMessage().'<br />Source filename of exception: '.$this->getFile().'<br />Source line of exception: '.$this->getLine();

}

}


Undeniably, the definition of the above "ResultException" subclass is pretty simple. It extends the functionality of the corresponding "Exception" parent; it implements a basic method called "showResultExceptionInfo()." This method returns specific information to client code about the launched exception, including its source file, the line of code that triggered the error, and so forth. Nothing too complicated to grasp, right?

Well, now that we have derived a subclass that handles specific MySQL result set exceptions, please focus your attention on the following script. It demonstrates how to use the subclass. 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 />';

}

// throw a Result Exception

echo $result->getInsertID();

 

/* displays the following

Catching Result exceptions...

Exception message: Error getting ID

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

Source line of exception: 93

*/

}

// catch MySQL exceptions here

catch(MySQLException $e){

echo $e->showMySQLExceptionInfo();

exit();

}

// catch Result exceptions here

catch(ResultException $e){

echo $e->showResultExceptionInfo();

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 you can see from the above hands-on example, the "getInsertID()" method that belongs to the "Result" class is called deliberately. This condition immediately fires up an exception of type "ResultException," which is finally intercepted and handled property by a specific "catch" block.

Hopefully, the previous example was useful enough to demonstrate how several types of exception can be handled within the same PHP 5 application.

Feel free to use all of the code examples developed in this tutorial to help you acquire a more solid background in working with exception subclasses within PHP 5-driven applications.

Final thoughts

In this third installment of the series, you learned how to combine three different types of exceptions in the same PHP 5 application. In this particular case, the whole process demanded that we first derive two specific subclasses from the base "Exception" class, and then include the corresponding "try-catch" blocks required to handle each kind of exception.

In the next article, I'm going to conclude this series by demonstrating how to merge the two exception subclasses that you have learned into one, which can be useful if you don't want to clutter the source code of a web application with many "try-catch" blocks.

Now that you've been warned about the topics that will be discussed in the final article, you won't want to miss it!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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