If you check the code, then you will find that for a connection failure we throw
a PGSQLConnectionException object, and for a query failure we just throw a
PGSQLQueryException object. We can custom develop these objects by extending
the core Exception class of PHP5. Let's take a look at the code. The first one is the
PGSQLConnectionException class.
Class PGSQLConnectionException extends Exception
{
public function __construct()
{ $message = "Sorry, couldn't connect to postgresql server:";
parent::__construct($message, 0000);
}
}
?>
And here comes PGSQLQueryException class
Class PGSQLQueryException extends Exception
{
public function __construct($connection)
{
parent::__construct(pg_last_error($connection),0);
}
}
?>
That's it!
Collecting all PHP Errors as Exception
If you want to collect all PHP errors (except the FATAL errors) as exception, you can
use the following code:
function exceptions_error_handler($severity, $message,
$filename, $lineno) {
throw new ErrorException($message, 0, $severity,
$filename, $lineno);
}
set_error_handler('exceptions_error_handler');
?>
Chapter 3
[ 53 ]
The credit of the above code piece goes to fjoggen@gmail.
Pages:
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74