NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP 5 Exception Expertise Needed

Joseph Crawford codebowl at gmail.com
Thu Mar 17 15:09:59 EST 2005


Hello Everyone,

I have been working with the Exceptions in PHP 5 and i cannot figure
out why i am getting these results nor can i figure out how to trace
the resutls.  Let me start with the code files


This is my main exception class.

<?php
/**
* The MysqlEx class
*
* This is the exception class for the main Application class
*
* @author Joseph Crawford Jr. <codebowl at gmail.com>
* @copyright Codebowl Solutions 2005
* @package SimonVolkov
* @subpackage Exceptions
*
**/
class SvEx extends Exception {
	
	const ERR_TYPE = "Unknown Error";
	const ERR_DESC = "There has been an error. This error has been
logged. Please try again in a few minutes.";
	
	
	protected $_msg;
	protected $_tpl;
	protected $_error;
	
	
	public function __construct( $error = null ) {
		
		$this->_error = array(
			'type' => self::ERR_TYPE,
			'description' => self::ERR_DESC
		);
		
		$this->_tpl = Application::Template();
		$this->_tpl->assign( 'error_type', $this->_error['type'] );
		$this->_tpl->assign( 'error_description', $this->_error['description'] );
		$this->_msg = $error;
	
		parent::__construct($this->_msg);
	}
	
	public function Display() {
		$this->_tpl->assign('admin', 1);
		$this->_tpl->assign('msg', $this->_msg);
		$this->_tpl->display('error.tpl');
	}
}
?>

from this i extend for each exception type that i have

here is the application exception

<?php
/**
* The MysqlEx class
*
* This is the exception class for the main Application class
*
* @author Joseph Crawford Jr. <codebowl at gmail.com>
* @copyright Codebowl Solutions 2005
* @package SimonVolkov
* @subpackage Exceptions
*
**/
class ApplicationEx extends SvEx {
	
	const ERR_TYPE = "Application Error";
	
	public function __construct( $msg = null ) {
		
		parent::__construct($msg);
		
		$this->_error['type'] = self::ERR_TYPE;
		
	}
}
?>

when i throw a new ApplicationEx for some reason the
$this->_error['type'] is not getting set to Application Error but yet
Unknown Error.

you can see an application error thrown on this page for testing

http://codebowl.homelinux.net:8001/clients/JVMedia/SimonVolkov/global.php

the Error Reported part is going to be hidden from the end user and
only shown to admins and locally during development.

here is the line that is throwing the ApplicationEx Exception

if( ( isset( $dbinfo['type'] ) && $dbinfo['type'] != "" ) && ( isset(
$dbinfo['host'] ) && $dbinfo['host'] != "" ) && ( isset(
$dbinfo['database'] ) && $dbinfo['database'] != "" ) ) {
     self::$_db = new $dbinfo['type']($dbinfo['host'],
$dbinfo['database'], $dbinfo['user'], $dbinfo['pass']);
} else {
     throw new ApplicationEx("Could not instanciate the database
object, you must specify the database type, hostname and database
name.");
}

and here is the code to catch the exceptions

try {
	
	$app = new Application( $dbinfo );
	
} catch(MysqlEx $mysqlEx) {
	
	// show the error message
	$mysqlEx->Display();
	
} catch( ApplicationEx $appEx ) {

	// show the error message
	$appEx->Display();
	
} catch(Exception $e) {
	
	// show the error message
	$e->getMessage();
	
}

any help with this would be appreciated.  I have tried to debug using
zend studio however using the __autoload magic function zend studio
says fatal error class SvEx cannot be found.


-- 
Joseph Crawford Jr.
Codebowl Solutions
codebowl at gmail.com



More information about the talk mailing list