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!
blog comments powered by Disqus |
|
|
|
|
|
|
|