There are places in our cmw code where we are handling errors for example here in the contact page.
There are several other places in our cold though where errors and exceptions are though unlikely could
occur.
It's not a good idea to leave these unhandled.
So let's add area and exception handlers that will deal with these if they occur.
As we were including this initial script on each page this is a good place to put them.
We'll start by adding an error handler.
Which as we saw in the previous video just throws an error exception to past the handling ones the exception
handler function will add some code in here in a moment.
Then we set the error handler an exception handler to use these functions.
Inside the exception handler function we have access to an exception object.
The exception object has several methods to get more details about the exception the message the code
number the name of the script where it occurred and the line it occurred on and so on.
So we can fix the error we want as many details as possible.
So let's print out a header then paragraphs with the class of the exception using the get class function
to get the class of the Exception object.
The message.
The stack trace.
Which is a list of the function calls made inside the script up to where the error occurred.
Which will print out inside price tags so their formative correctly.
And finally the file and line number.
Finally it's always a good idea to terminate the script execution in the handler.
At this point to test this.
Let's break our code temporarily here in the Get page method of the article class.
Let's make an error in the ask you well.
This function is called on the index page of our site.
When we visit the index page we get the full details of the error that's occurred the class the message
the stack trace and the file and line number where it occurred.
This information is extremely useful to us when debugging our code so that we can fix the error.
However we don't want these details to be shown when the site is in production on the life server on
the Internet.
Apart from looking unprofessional revealing all these data about your site and code is a security issue.
To fix this.
First let's add a config setting to the configuration file called show error detail that can be true
or false.
Then in the exception handler if this constant is true we'll display the detail as we're doing now.
If it's false we'll just display a message with no details.
With this set to true.
The Aero details are shown as before.
If I change this to false.
Then the error details aren't shown.
So in addition to the database and S M T P server credentials we can change this setting.
Depending on the environment while we are developing we'll set it to true.
And let's not forget to fix the temporary error in the article class code.
And now all is working again.