Normally you can't catch an error... only exceptions.... luckily with PHP you can set up an error_handler that does throw exceptions. (from comments on php ErrorException page)
class ErrorHandler extends Exception {
protected $severity;
public function __construct($message, $code, $severity, $filename, $lineno) {
$this->message = $message;
$this->code = $code;
$this->severity = $severity;
$this->file = $filename;
$this->line = $lineno;
}
public function getSeverity() {
return $this->severity;
}
}
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorHandler($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler", E_ALL);
So for your specific problem you would just do:
try{
$result = $db->execute($query);
$primary = $result->getRows();
} catch (ErrorHandler $e){
header("Location: /index.php");
exit;
}