Page 1 of 1

trying to make exception work, please help

Posted: Thu Nov 09, 2006 11:10 pm
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

--------------------------------------------------------------------------------

Posted: Fri Nov 10, 2006 3:45 am
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.

Posted: Fri Nov 10, 2006 3:53 am
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().

Posted: Fri Nov 10, 2006 8:02 am
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

Posted: Fri Nov 10, 2006 9:14 am
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?