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 PHP Standards and Best Practices!
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Learn how to convert errors in your code into exceptions
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
Previously, we discussed the differences
between errors and exceptions.
0:00
PHP can do something pretty cool, and it
converts errors into exceptions.
0:04
The benefit here is that sometimes an
error is unavoidable.
0:09
For example, if you try to read a file
that cannot be read,
0:12
it could throw an error.
0:15
If you check to see if that file exists
before you try to read it,
0:17
then you can avoid the error.
0:20
But, in the milliseconds between checking
to see if that file exists and
0:22
actually trying to read that file, it
could have been deleted.
0:25
This is called a race condition.
0:29
And they're no fun at all.
0:30
Sometimes, you need to try and do
something and recover if it goes wrong.
0:32
Converting errors to exceptions lets you
do that.
0:36
Let's take a look and see how we do that
with a bit of code.
0:39
Here we have a tiny snippet of code.
0:42
On line four here as we've seen in many
other videos,
0:45
we're turning our display errors on.
0:47
You might not always have to do this
yourself, but on workspaces we do.
0:48
On line seven, we're trying to open a file
called nope.txt,
0:53
which in this example doesn't exist.
0:56
The code tries to deal with the file maybe
not existing on lane ten,
0:59
by checking to see if the handle variable
is set to False.
1:02
But at that point, PHP would already have
thrown an error.
1:05
Let's try this code out by clicking on the
eye icon for a preview.
1:09
So, here we have a warning from the F open
core function saying failed to
1:12
open the stream.
1:16
No such file or directory.
1:17
Now this is annoying, it's displayed a
error message even though we are trying to
1:18
display our own error message, and that's
what this is here.
1:23
Normally it'd look a little bit prettier,
but this is just an example.
1:26
So, how do we convert errors to exceptions
here in a way that can helps us?
1:29
Well, PHP has a function called Set Error
Handler,
1:33
which accepts an anonymous function as a
callback.
1:36
Every time a PHP error of any severity is
about to be thrown,
1:39
instead of doing what PHP normally does,
it'll run this callback.
1:42
Let's have a look at how this callback
works with a really simple error handler
1:45
that throws an exception.
1:48
If we go back to our work spaces here, and
1:49
just make a little space, and throw the
set error handler in place.
1:52
So, here we have an anonymous function,
and it accepts these four arguments.
1:58
We have error no,
2:04
which is a number relating to the type of
error that's being thrown.
2:05
We have error string here, which contains
a human readable message,
2:10
which will explain what the error is.
2:13
We have error file here.
2:16
And then at the end we have error line.
2:18
All of these arguments are passed through,
and
2:20
as we should be familiar with, we are
throwing a new error exception.
2:23
This error exception is a core PHP
exception type, and it's designed for
2:27
exactly this sort of behavior.
2:30
Then we simply pass on all the arguments
in this specific order, and
2:32
we should be good to go.
2:36
Let's save this and see what happens when
we run it.
2:36
[BLANK_AUDIO]
2:39
Oh, no.
2:41
That's quite hard to read.
2:42
Let's view the page source.
2:43
[LAUGH] So now we have a fatal error.
2:44
That's a bit worse than a warning, so
let's see what's going on.
2:47
There's an uncaught exception of error
message, with the message.
2:50
And this is the same message we had
before.
2:55
Right?
This was the warning we had but
2:56
it's now been converted to a error
exception.
2:58
And we're not catching it.
3:00
Let's go back to our code and see if we
can work out a way to catch it.
3:02
Now, when working with exceptions, you
need to put anything
3:06
contentious in a try block, and then the
result needs to go in a catch block.
3:11
Let's just get this comment out of the
way, make life easier.
3:16
[BLANK_AUDIO]
3:18
So, we want to catch an error exception.
3:21
We want to assign it to an e variable.
3:27
Whether we use this or not, we have to
assign it to a variable.
3:29
So now we are trying to read the file.
3:32
This may throw an exception and
3:34
if it does, we're getting ready to catch
it right here.
3:36
Let's try this out and see if we got it
right.
3:39
[BLANK_AUDIO]
3:40
Perfect!
3:43
Now we have no warnings, no fatal errors,
nothing weird on the screen.
3:43
When fOpen comes across a problem, it will
throw an exception.
3:47
And we know something went wrong, and we
can handle the error properly.
3:50
We can do all of this without having to
stoop to hiding errors in development,
3:53
which is a huge pain and
3:56
can really cause problems when you're
trying to debug something.
3:57
It might not always make sense to convert
errors to exceptions.
4:01
In a situation where you're building a
component which could be installed on
4:04
anyone's application, you might not be
able to rely on them being converted.
4:07
But in your own applications,
4:11
you can absolutely do this as you have
total control.
4:12
Exceptions can help you catch tricky stuff
in development, and
4:15
help you work around some of those errors,
4:18
where PHP would rather keep on trucking
than stop processing.
4:20
Exceptions make PHP complete and more
noticeably, and more conveniently, and
4:23
that's usually a really helpful thing.
4:27
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