RE: Exception Handling

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



-----Original Message-----
From: James Colannino [mailto:crankycyclops@xxxxxxxxx] 
Sent: Friday, June 01, 2012 10:25 AM
To: PHP-General List
Subject:  Exception Handling

Hey guys,

Haven't posted in a long time...  Happy Memorial Day!  I have an issue
with exception handling.  I'm using a framework that throws a
"Database_Exception" object.  I was expecting catch (Exception $var) to
be sufficient to catch this, but it doesn't.  I have to do catch
(Database_Exception $var) to make it work.  I've been reading that
Exception should be sufficient, since every exception object is a child
class of Exception.  Did this perhaps change in 5.4?  I think it was
working properly in 5.3, but I'm not sure.

Any clarification would be greatly appreciated!

James

--

Hi James,

You would have to catch Database_Exception. It is also good practice to
also always catch exception afterwards.

Ex:

Class Database_Exception extends Exception{
	Public function __construct($message){
		parent::__construct($message);
	}
}

Try{
	$pdo = new PDO();
	If($pdo === false){
		Throw new Database_Exception("Failed To Connect");
	}
}
Catch(Database_Exception $e){
	Echo $e->getMessage();
}
Catch(Exception $e){
	Echo $e->getMessage();
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux