When we connect to the database using my Escuela.
We can detect if there's a connection error using the my ESC You will connect ever function in pdl.
There isn't an equivalent function.
However instead it uses the object oriented way of dealing with errors.
P HP uses exceptions to handle specific error conditions.
If an error occurs in some code that code can throw an exception object when this occurs we can catch
it and take the necessary action.
Let's look at an example at the moment.
This PDA code is working.
If I change the password dull for example
so that it's now incorrect.
In the browser we get a fatal error that says one cart PDA an exception and it tells us where this is
happening in the pdl Constructor.
The constructor is kalid when we create a new object of this class to handle exceptions.
First we enclose the code that could cause an exception in a try block.
Then we add a matching catch block which has an argument which is the exception object that's been thrown.
This exception object is an object of the exception class R of a child class of this class so this object
will have the get message method which returns the detailed error message of the error that's occurred.
So if an exception is thrown when we try to create a Pedia object let's print out the error message
and then exit the script.
Now when we ran the code in the browser we get the error message saying Access denied because the password
is wrong and the script stops.
If I correct the error.
The page is working again.
There's another place that we're checking if an error occurs here in the index script.
If the query method returns FALSE something has gone wrong with the query.
We're checking this and then dumping out this array which contains the error details.
If I make a change to the esky well so that it's invalid
when we run this code we get an array that does contain the error message.
It would be easier if this code threw an exception so that we could trap it in a try catch block.
We can configure PD 0 to do just this by setting this attribute so that if an error occurs when working
with the database an exception is thrown.
We set this attribute under PD o object.
So here in the Database class instead of returning it directly Let's assign it to a variable then sets
the attribute that throws exceptions and then finally return the object as before.
Now in the browser with the error in our esky Well we get an exception.
So we could wrap this query line in a try catch block to catch the exception and print out the details
of the error as we're doing here in the Database class.
However there's not much point in just catching exceptions everywhere.
If all we're going to do is just print out the error message later on will look at how to add a general
exception handler that catches on cart exceptions and Lexa's deal with them in one place so we can remove
these lines that check the result of the query method.
As we'll deal with any exceptions later run.
This also makes our code much cleaner which is always a good thing.
Finally let's fix the error in the esky real time introduced earlier.
Which means that in the browser our index page is working again.