Cool Penguin

PersistClass for PHP - keeping SQL simple

Tutorial

Error handling

PersistClass throws exceptions if any error occurs, making sure no errors remain invisible. The following examples show how to catch some error exceptions. Refer to the documentation to see all exception types.

Catching SQL query errors

$con = DbConnectionPool::instance()->getConnection();
try {
  $con->query("This is not a valid sql query");
} catch(QueryException $e) {
  // thrown when query fails
} 

Catching connection errors (ConnectionException)

try {
  $myConnection = new DbConnectionMysql();
  $myConnection->connect('localhost', 'demouser', 'demopassword', 'demodatabase');
  DbConnectionPool::instance()->registerConnection($myConnection);
} catch(ConnectionException $e) {
  // thrown when there is a problem with connection
} 

Catching errors of trying to retrieve non-existing row/object or trying to read data from non-existing column

try {
  $test = new TestTable(1);
  echo $test->getData('thiscolumndoesntexist');
} catch(NoDataException $e) {
  // thrown when referring to non-existing columns
} catch(NoResultException $e) {
  // thrown when trying to initiate non-existing object
}  
Check out the next example on