Exceptions problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Exceptions problems

Post by someberry »

Ok, so I have read the PHP manual and tried out their examples, but I cant for the life of me just make it come out with an error such as:

Code: Select all

DBError: You do not seem to have database permission.
It always comes out as:

Code: Select all

Fatal error: Uncaught DBError: You do not seem to have database permission. thrown in D:\localhost\htdocs\classes\Connection.class.html on line 45
This is the code I am using:

DBError class

Code: Select all

class DBError extends Exception
{
	function __construct($message)
	{
		parent::__construct($message, 0);
	}
	
	public function __toString()
	{
		return  __CLASS__ . ': ' . $this->message;
	}
}
Connection.class.html

Code: Select all

$this -> connection = @mysql_connect ('localhost', 'user', 'password', true);
		
if(!$this -> connection)
{
	throw new DBError('You do not seem to have database permission.');
}
Does anyone know why it is doing this?

Thanks,
someberry.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

thrown exceptions should be caught:

Code: Select all

function myFunctionThatThrowsAnError() {
      throw new DBException('Not Authorised');
}

try {
      myFunctionThatThrowsAnError();
}

catch (DBException $e) {
      die "Caught: ".$e->getMessage();
}

//continue...
Post Reply