Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Basic Exception Handling with PHP!
You have completed Basic Exception Handling with PHP!
Preview
There are times when we would like our application to throw its own exceptions. Such as when a resource, like a file or a database, that SHOULD be available isn't.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We've looked at the simple example
of how to catch exceptions
0:00
that php itself throws.
0:03
There are other times when we would
like our application to throw its
0:05
own exceptions as well.
0:09
Such as when a resource, like a file or a
database that should be available, isn't.
0:11
Remember, this should not be used in
the normal flow of an application, but
0:17
only when something exceptional happens.
0:22
Let's go into work spaces and
throw our own exceptions.
0:24
Let's try reading a file
that doesn't exist.
0:28
Start with a new file named data.php.
0:31
Open your php tag and echo a line for
the end of the file so
0:37
we can see what gets processed.
0:42
Now we can add a line to read the file.
0:48
file = F open,
0:54
data.txt.
0:57
We'll open this for
reading and close the line.
1:02
Let's view this in the browser.
1:06
If error reporting is turned on for
1:12
warnings, we will get a warning,
no such file or directory.
1:14
Because it's simply a warning and
not a fatal error,
1:18
the script will continue to process,
which is why we see End of File.
1:21
On a production server we don't
want to show errors at all.
1:25
So let's stop displaying errors.
1:29
Ini_set display errors off.
1:36
Now when we refresh the page,
we don't see any errors at all.
1:44
It looks like everything ran properly,
but that's not actually what happened.
1:49
Exceptions allow us to catch issues and
control what happens.
1:53
Let's throw an exception instead.
1:57
If we can't open our file for reading,
Then we're going to throw a new exception.
2:03
Just like instantiating
a new class object,
2:11
we'll use the keyword new and
the class exception.
2:14
We can also pass an argument.
2:18
The message, unable to access file.
2:20
Instead of assigning this
to an object variable
2:27
we're going to use the keyword throw.
2:30
Let's go back to the browser.
2:33
PHP will halt the flow of the script and
2:37
attempt to find the first
matching catch block.
2:40
Just like with exceptions thrown by PHP,
if the exception is not caught,
2:42
a PHP fatal error will be issued
with an uncut exception message.
2:47
Since we have display errors turned off,
we won't see the error.
2:52
But we get either this ugly error page or
a blank screen,
2:55
because our scripts stops processing and
we can't get to the end of the file.
2:59
Let's go back to work spaces.
3:03
Let's comment out this line so
our display errors is back on.
3:08
Then we only want to display,
fatal errors.
3:13
So we can set error_reporting(1).
3:15
Now when we refresh the browser
we see the uncaught exception.
3:26
If we're going to throw errors,
we need to catch them.
3:30
So let's surround this
in a try catch block.
3:33
Try.
3:41
Catch.
We'll catch the exception.
3:46
And assign it to e.
3:52
And then we can do our exception handling.
3:56
For now we'll echo.
3:59
E.
4:02
Get message.
4:03
Let's view this in the browser again.
4:06
Now the code performs are exception
handling by showing the error and
4:11
allowing the script to continue.
4:14
This works, but it's not really
how we would use exceptions.
4:16
Since we'll be using exceptions
with object oriented code,
4:20
let's create a simple class and
read the file in a method called Get Data.
4:23
Class, my data.
4:33
And then our method,
using function, getData.
4:38
Now we can instantiate a new object and
4:51
place the call to the Get Data
method within the try catch block.
4:53
Data = new myData.
4:57
And then try Data, getData.
5:05
Let's go back to the browser and
refresh the page.
5:16
We see the exact same thing we
did last time which is great.
5:21
We're still handling the exception but
this time using a class and
5:24
object which is how will be handling
exceptions in our application.
5:28
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up