Home arrow PHP arrow Page 2 - Intercepting Customized Exceptions in PHP 5

Triggering customized exceptions in PHP 5 - PHP

Welcome to the second part of a four-part series on sub classing exceptions in PHP 5. In the first part of this series, you learned how to build a basic processing class which could throw customized exceptions to client code. What you haven't yet learned, however, is how to build the code to catch these exceptions. That is the topic of this article.

  1. Intercepting Customized Exceptions in PHP 5
  2. Triggering customized exceptions in PHP 5
  3. Deriving a subclass from the built-in Exception class
  4. Intercepting some MySQL-related exceptions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
October 22, 2008

print this article



As I stated in the introduction, building a customized exception mechanism with PHP 5 involves two basic steps. First, we must define one or more classes that have the ability to trigger several specific exceptions when something goes wrong. Second, we need to derive the corresponding subclasses from the built-in "Exception" class, which must catch all of these exceptions.

As I mentioned before, in the preceding tutorial I showed you how to take the first step; I defined a MySQL-processing class, which was provided with the capacity for launching a few MySQL-related exceptions.

If the signature of this particular class doesn't ring any bells for you, here it is:

// define 'MySQL' class

class MySQL{

private $conId;

private $host;

private $user;

private $password;

private $database;

private $result;

const OPTIONS=4;

public function __construct($options=array()){


throw new MySQLException('Invalid number of connection parameters');


foreach($options as $parameter=>$value){


throw new MySQLException('Invalid parameter '.$parameter);






// connect to MySQL

private function connectDB(){


throw new MySQLException('Error connecting to the server');



throw new MySQLException('Error selecting database');



// run query

public function query($query){


throw new MySQLException('Error performing query '.$query);


return new Result($this,$this->result);



// define 'Result' class

class Result {

private $mysql;

private $result;

public function __construct($mysql,$result){




// fetch row

public function fetchRow(){


return false;


return $row;


// count rows

public function countRows(){


throw new Exception('Error counting rows');


return $rows;


// count affected rows

public function countAffectedRows(){


throw new Exception('Error counting affected rows');


return $rows;


// get ID of last-inserted row

public function getInsertID(){


throw new Exception('Error getting ID');


return $id;


// seek row

public function seekRow($row=0){


throw new Exception('Invalid result set offset');



throw new Exception('Error seeking data');




As you can see, the previous "MySQL" abstraction class will throw some exceptions of type "MySQLException" when failing to connect to the server and selecting a specific database, or when running a SQL query.

So far, the way this sample class has been defined is fairly understandable to you, right? The next thing I'm going to teach you will be how to create a customized exception class, whose task will be to catch the aforementioned MySQL-related exceptions.

The full details of how this will be done will be covered in the next section, so click on the link below to keep reading.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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