Explain exceptions to me :)

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Explain exceptions to me :)

Post by daedalus__ »

I've been looking at alot of stuff but still don't quite understand the process..

I know I wrap blocks in try { } and then 'catch' exceptions but where are those exceptions defined and how do i throw them?

examples, examples, pretty please with super shooger on top
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

The Exception class is a built-in PHP Class.

Code: Select all

try {
      // Connect to DB
      $db = mysql_connect($host, $user, $pass);

      if (!$db) {
          // Whoops it failed
          throw new Exception(mysql_error());
      }
} catch (Exception $e) {
    // Print error
    print $e->getMessage();
}
getMessage is a method in the Exception class which returns the first paramater you pass to Exception. (In this case mysql_error()).
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

I learnt everything I know about exceptions here: http://www.zend.com/php5/articles/php5-exceptions.php
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

A snippet from one of my classes:

Code: Select all

class ConnectionException extends Exception {}
class MySQLException extends Exception {}

class MySQLDatabase extends AbstractDataBase implements iDatabase
{
    public function __construct ($host, $user, $pass, $db = null)
    {
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->db = $db;
    }

    public function connect()
    {
        $this->resource = @mysql_connect($this->host, $this->user, $this->pass);

        if (!$this->resource) throw new ConnectionException('Could not connect to MySQL Database: ' . mysql_error());

        if (!is_null($this->db)) $db = @mysql_select_db($this->db, $this->resource);

        if (!$db) throw new MySQLException('Database could not be selected: ' . mysql_error());
    }

//snip

}
Then when used..

Code: Select all

include_once 'includes/classes/AbstractDataBase.class.php';
include_once 'includes/classes/MySQLDatabase.class.php';
include_once 'includes/interfaces.inc.php';

try {
    $db = new MySQLDataBase ('host', 'user', 'pass', 'db');
    $db->connect();
} catch (ConnectionException $e) {
    Error::LogError($e->getMessage());
    die ('There has been an error connecting to the Database');
} catch (MySQLException $e) {
    Error::LogError($e->getMessage());
    die ('There has been an error with the MySQL Database');
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

viewtopic.php?t=50490 may be of interest.
Post Reply