//class.db.php
error_reporting(E_ALL - E_WARNING);
class db
{
function connect()
{
if (!pg_connect("host=localhost password=pass user=username
dbname=db")) throw new Exception("Cannot connect
to the database");
}
More OOP
[ 50 ]
}
$db = new db();
try {
$db->connect();
}
catch (Exception $e)
{
print_r($e);
}
?>
The output will be something like this:
Exception Object
(
[message:protected] => Cannot connect to the database
[string:private] =>
[code:protected] => 0
[file:protected] => C:\OOP with PHP5\Codes\ch3\exception1.php
[line:protected] => 8
[trace:private] => Array
(
[0] => Array
(
[file] => C:\OOP with PHP5\Codes\ch3\exception1.php
[line] => 14
[function] => connect
[class] => db
[type] => ->
[args] => Array
(
)
)
[1] => Array
(
[file] => C:\Program Files\Zend\ZendStudio-
5.2.0\bin\php5\dummy.php
[line] => 1
[args] => Array
(
[0] => C:\OOP with PHP5\Codes\ch3\exception1.php
)
[function] => include
)
)
)
Chapter 3
[ 51 ]
So you get a lot of things in this exception class. You can catch all the errors using
this try-catch block. You can use try-catch inside another try-catch block. Take a look
at the following example.
Pages:
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72