trying to make exception work, please help

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
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

trying to make exception work, please help

Post by iffo »

Hi,
I have not used eception that much

please tell me what i am doing wrong here, My function which gives error is

Code: Select all

public static function find_all($class_name, $where="1", $args="")  
    {  
        // check to make sure that the class passed actually exists.  
        if (!class_exists($class_name))  
            throw new NonExistantClassException('The class specified for RecordBase::find_all() does not exist.');  
      
        // extract all of the classes variables to get its primary key.  
        extract(get_class_vars($class_name));  
          
        // build the sql statement used to get the desired rows.  
        $sql = "SELECT * FROM `{$table_name}` WHERE {$where} {$args['group_by']} {$args['order_by']} {$args['having']} {$args['limit']}";  
          
        // calls the method to get the records we are searching for.  
        return self::find_by_sql($class_name, $sql, $args);  
    }
I am calling it like this

Code: Select all

try{ 
$this->sites = Buyer::find_all("Buyer"); 
}     
catch (NonExistantClassException $e )  
{  
   // mail("you@site.com", "Query Error", $e->message);  
    die ( "I'm afraid there was an error with the database server, please contact the help desk.");  
}

I still get
Fatal error: Uncaught exception 'NonExistantClassException' with message 'The class specified for RecordBase::find_all() does not exist.' in /home/irfan/projects/sitemanager2/lib/helpers/record_base.php:298 Stack trace: #0 /home/irfan/projects/sitemanager2/app/common/controllers/basic_site_controller.php(39): RecordBase::find_all('Site', 'url = 'extended...') #1 /home/irfan/projects/sitemanager2/public/index.php(107): BasicSiteController->__construct() #2 {main} thrown in /home/irfan/projects/sitemanager2/lib/helpers/record_base.php on line 298

--------------------------------------------------------------------------------
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
class NonExistantClassException extends exception {
}

class RecordBase {
	public static function find_all($class_name, $where="1", $args="") 
	{ 
		// check to make sure that the class passed actually exists. 
		if (!class_exists($class_name)) 
			throw new NonExistantClassException('The class specified for RecordBase::find_all() does not exist.');  

		// extract all of the classes variables to get its primary key. 
		extract(get_class_vars($class_name)); 

		// build the sql statement used to get the desired rows. 
		$sql = "SELECT * FROM `{$table_name}` WHERE {$where} {$args['group_by']} {$args['order_by']} {$args['having']} {$args['limit']}"; 

		// calls the method to get the records we are searching for. 
		return self::find_by_sql($class_name, $sql, $args); 
	} 
}


class BasicSiteController {
	public function __construct() {
		try{
			$sites = RecordBase::find_all("Buyer");
		}     
		catch (NonExistantClassException $e ) 
		{ 
			// mail("you@site.com", "Query Error", $e->message); 
			die ( "I'm afraid there was an error with the database server, please contact the help desk."); 
		}
	}
}

$b = new BasicSiteController;
works fine for me. The output is
I'm afraid there was an error with the database server, please contact the help desk.
gunman
Forum Newbie
Posts: 10
Joined: Fri Aug 26, 2005 12:07 pm

Post by gunman »

In zend framework i assume you are using it in your example you should define NonExistantClassException as extended class of Exception. Because of that it gives you that error. Also you could get your throwed exception with calling method $e->getMessage().
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

Post by iffo »

thanks you so much for your help. I have one question. You know I have function

Code: Select all

public static function find_all($class_name, $where="1", $args="")
	{
		
		
				
		
		// check to make sure that the class passed actually exists.
		if (!class_exists($class_name))
			throw new NonExistantClassException('The class specified for RecordBase::find_all() does not exist.');
	
		// extract all of the classes variables to get its primary key.
		extract(get_class_vars($class_name));
		
		// build the sql statement used to get the desired rows.
		$sql = "SELECT * FROM `{$table_name}` WHERE {$where} {$args['group_by']} {$args['order_by']} {$args['having']} {$args['limit']}";
		
		// calls the method to get the records we are searching for.
		return self::find_by_sql($class_name, $sql, $args);
	}
where I am throwing exception.

and in the program where I called that function that where i have my try{} catch ...

Code: Select all

try{ 
$this->sites = Buyer::find_all("Buyer"); 
}      
catch (NonExistantClassException $e )  
{  
   // mail("you@site.com", "Query Error", $e->message);  
    die ( "I'm afraid there was an error with the database server, please contact the help desk.");  
}


My question it it possible to do this try catch thing with in that function find_all()?

Thanks
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

Post by iffo »

I have a question, normally we have if statement based on which we throw exception

if (!class_exists($class_name))
throw new NonExistantClassException('The class specified for RecordBase::find_all() does not exist.');

Now in my case if I have generated a wrong sql , based on which I am having error, how can i put that in if statement, like have above?
Post Reply