Steffen Goeschel Sun Apr 29 03:26:57 -0400 2012

Subject: Problems with error handling

I am working on a codeigniter 2 project with php-activerecord models. I am having troubles with some models and I can't seem to find a way to catch errors before they crash my application. Here is a test example:

My Model looks like this:

class Cohort extends ActiveRecord\Model
{
static $has_many = array(
array('course_outcomes')
);
}

My controller function does this:

try
{
$this->view_data['test'] = Cohort::find(1);
}
catch (\ActiveRecord\RecordNotFound $e) {
echo $e->getMessage();
}

I have also tried this:

try
    {
if($cohort = Cohort::find(1)) {
$this->view_data['test'] = $cohort;
}
else {
throw new \ActiveRecord\ActiveRecordException('It hasn't worked.');
}
}catch(\ActiveRecord\ActiveRecordException $e) {
echo $e->getMessage();
}

This works if Cohort::find(1) returns a result, which is the case if I comment out the association. However, something in my association breaks the query, but that's not my question. My question is why does the try...catch block not catch an exception? Whatever I do, an invalid query seems to immediately break my application and return a blank screen. Can anybody help?

PHP is version 5.3.6 and error reporting is turned on (and works for most other php errors).


Chris R Mon May 14 11:02:08 -0400 2012

What happens if you just catch all exceptions in your controller? (I'm not suggesting this as a solution, but it'd be useful to figure out if any exceptions at all are being thrown):

try
{
  $this->view_data['test'] = Cohort::find(1);
}
catch (Exception $e)
{
  echo $e->getMessage();
}

(1-1/1)