Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
An exception is an object that is “thrown” by your application in the event that something goes wrong. Catching exceptions allows us to recover from the error, or allow our application to end gracefully. We can customize what happens with each exception based on what type of exception we receive.
[MUSIC]
0:00
Hi, I'm Alena.
0:05
Exceptions are the object oriented
approach to handling errors.
0:07
An exception is an object that's
thrown by your application
0:10
in the event that something goes wrong.
0:14
This allows you to interrupt the flow
of the program and customize
0:16
how the application handles errors,
or gracefully exit the application.
0:20
When an exception is thrown it halts
0:26
the processing until the exception
is either caught or left on handled.
0:28
If left on handled the exception
stops the application.
0:33
This unhandled exception is the typical
behavior for a fatal error.
0:37
But what makes exceptions, exceptionally
useful is the ability to catch them.
0:40
Catching exceptions allows
us to recover gracefully and
0:46
either exit our application or
customize the error.
0:50
We can customize what happens based
on what type of exception we receive.
0:54
I can put the yellow balls in a bag,
the blue ball in a basket,
1:00
and the red ball in a box
to mail somewhere else.
1:05
In production, unhandled exceptions
who cause the page to stop loading, or
1:11
not load it all, but
catching exceptions gives you the ability
1:16
to determine what happens
with each exception.
1:20
You might redirect a user to
an error page, log an issue, or
1:23
do any other type of error handling.
1:27
Let's take a look at the syntax used for
1:30
catching database exceptions
with the PHP data object.
1:32
First, we have the keyword, try,
1:37
then within curly braces we have
the code we want to try, or
1:39
attempt to execute, in this case,
connecting to a sqlite database.
1:43
If there is an error in the connection,
such as spelling sqlite with two Ls,
1:49
the PTO class will throw an exception
that we catch in the next block.
1:54
After the keyword, catch,
we use parenthesis to tell PHP what
1:59
we'll be catching,
in this case, exception.
2:03
We then assign the exception that
was caught to a new variable, e.
2:07
Then we can use that exception variable to
get details using the exception methods.
2:13
We've used three methods for
2:18
this message, get message,
returns, could not find driver.
2:19
Get file,
returns the full path to the file.
2:25
And get line, returns the line
number where the exception occurred.
2:29
You wouldn't want all these
details displayed in production.
2:34
You would probably show a simple
error message and log more details.
2:36
You could also try a secondary
database connection at this point.
2:41
What happens in the catch
block is really up to you and
2:46
the requirements of your application.
2:49
There are additional methods available for
the exception class, so
2:50
make sure you check the notes associated
with this video for more details.
2:55
In the next few videos we'll cover more
details on how to use exceptions and
3:00
under what conditions.
3:05
But, first, here are four tips for
making proper use of exceptions.
3:07
First, exceptions are part of
object oriented programming.
3:12
Since an exception is an object itself,
3:16
it's really designed to work best with an
object oriented approach to programming.
3:19
PHP offers a number of options for
3:25
error handling when working
with procedural code.
3:27
In most cases, I recommend using
errors for procedural code and
3:30
exceptions when working with objects.
3:34
If you haven't seen
the workshop on error handling,
3:37
check the notes attach this video for
more details.
3:40
Second, exceptions are exceptional.
3:44
Don't use exceptions to
manage normal program flow.
3:48
A try catch block is not
an if else statement,
3:52
your application should still
function without the try catch block.
3:56
Third, exceptions are meant to be handled.
4:01
The very existence of a try catch block
4:04
indicates that exceptions
are meant to be handled.
4:07
They are meant to be resolved,
even if that resolution
4:10
is to rethrow the exception after
doing some sort of error handling.
4:14
With errors, fatal or
4:18
otherwise, there's the possibility
that something was completed halfway.
4:20
Exceptions help eliminate this possibility
by halting processing long enough for
4:25
you to clean up before
terminating the application.
4:30
For example,
closing the connection to a database, or
4:34
cleaning up half written file.
4:37
And finally,
exceptions are not meant to be silenced.
4:40
If you have an empty catch block,
you are silencing the exception.
4:44
The application will not be halted and
4:48
there is no error handling
in the catch block.
4:51
The exception simply disappears.
4:53
PHP throws exceptions because there's
a problem, and frameworks do the same.
4:57
Handle those exceptions,
don't just silence them.
5:02
Quietly handling exceptions is
not the same as silencing them.
5:06
If an exception is thrown because
a file does not exist, and
5:10
in your catch block you create that file,
5:14
the exception has now been handled and
your code may continue processing.
5:17
Exceptions are thrown for a reason.
5:23
Catch them, handle them,
don't ignore or silence them.
5:24
You need to sign up for Treehouse in order to download course files.
Sign up