WEBVTT

1
00:00:00.610 --> 00:00:03.430
Whenever your code relies
on its external system,

2
00:00:03.430 --> 00:00:06.690
there's always the chance that
something could go wrong.

3
00:00:06.690 --> 00:00:10.590
What if the database is down,
how should your code respond?

4
00:00:10.590 --> 00:00:12.670
These are known as exceptions and

5
00:00:12.670 --> 00:00:16.040
any code connecting to an external
system needs to handle them.

6
00:00:17.370 --> 00:00:21.210
Managing errors in any
application is very important,

7
00:00:21.210 --> 00:00:24.180
especially when it comes
to database errors.

8
00:00:24.180 --> 00:00:27.900
Remember, exceptions are exceptional,

9
00:00:27.900 --> 00:00:31.610
you shouldn't use them with code
that you're expecting to happen.

10
00:00:31.610 --> 00:00:36.450
If the code is functioning as designed,
it should never hit an exception.

11
00:00:36.450 --> 00:00:39.910
However, if the code encounters
something that will not

12
00:00:39.910 --> 00:00:44.250
allow it to function as intended,
then we want to catch an exception and

13
00:00:44.250 --> 00:00:47.460
most likely log it and
notify someone in some way.

14
00:00:48.805 --> 00:00:51.475
Any code that connects to
an external system should

15
00:00:51.475 --> 00:00:55.615
be placed inside a block of code
that can handle exceptions.

16
00:00:55.615 --> 00:01:01.475
In PHP the block of code for handling
exceptions is called a try-catch block.

17
00:01:01.475 --> 00:01:03.945
Let's go back to workspaces and
add that to our code.

18
00:01:05.455 --> 00:01:09.225
Before the code you want to try to
execute, you put a try command.

19
00:01:11.910 --> 00:01:15.410
You surround the code you want
to try in a set of curly braces.

20
00:01:17.400 --> 00:01:21.990
This tells PHP to try to execute all
the code inside those curly braces.

21
00:01:23.010 --> 00:01:26.850
You then put a catch command
after the try block.

22
00:01:26.850 --> 00:01:30.900
When the exception occurs, an exception
will get passed to the catch block.

23
00:01:32.130 --> 00:01:36.912
After catch you type a set of parentheses
with the following code inside them

24
00:01:36.912 --> 00:01:41.500
(Exception $e), which

25
00:01:41.500 --> 00:01:45.580
will catch the exception that occurred and
put the details into the variable e.

26
00:01:47.150 --> 00:01:49.470
Finally, you put another
set of curly braces.

27
00:01:50.750 --> 00:01:54.930
If there is an exception, the code inside
these curly braces will get executed.

28
00:01:56.030 --> 00:02:01.789
If our code above inside the try
block cannot connect to the database,

29
00:02:01.789 --> 00:02:06.875
we can display a detailed
message using $e->getMessage.

30
00:02:10.344 --> 00:02:13.476
Then stop any more code from
executing with the exit command.

31
00:02:15.880 --> 00:02:20.010
Just to recap, this block of code
tries to create an object from the PDO

32
00:02:20.010 --> 00:02:22.570
class connecting to our database.

33
00:02:22.570 --> 00:02:25.360
If there is a problem,
an exception is thrown and

34
00:02:25.360 --> 00:02:28.560
the code inside our
catch block is executed.

35
00:02:28.560 --> 00:02:32.320
This message here is displayed and
our code ends.

36
00:02:32.320 --> 00:02:36.130
If the connection is successful,
the catch block would be skipped and

37
00:02:36.130 --> 00:02:39.378
any code after this block
would continue to be executed.

38
00:02:39.378 --> 00:02:45.070
Remember, the try-catch block
is not an if-then statement.

39
00:02:45.070 --> 00:02:48.180
We do not want the code in
our catch block to run.

40
00:02:49.470 --> 00:02:52.070
Let's see what happens when we run
the script in the console now.

41
00:02:53.530 --> 00:02:57.250
Since the information needed to
connect to the database is incorrect,

42
00:02:57.250 --> 00:02:59.110
an exception is thrown.

43
00:02:59.110 --> 00:03:02.390
The same thing would happen if
the database server were offline.

44
00:03:02.390 --> 00:03:05.820
The code in the catch block echoes
out this message and exits.

45
00:03:07.100 --> 00:03:09.979
Let's go back and fix the connection
string and run the script again.

46
00:03:16.046 --> 00:03:21.040
Because there was no exception thrown, the
code inside the catch block is never run.

47
00:03:21.040 --> 00:03:26.010
And the code is executed to var_dump,
this newly created database object.

48
00:03:26.010 --> 00:03:30.720
No matter what error occurs,
I want PDO to throw an exception so

49
00:03:30.720 --> 00:03:32.820
we can catch it and fix it.

50
00:03:32.820 --> 00:03:33.660
So let's go ahead and

51
00:03:33.660 --> 00:03:39.350
do that by calling set attribute on
our new PDO object, the variable db.

52
00:03:39.350 --> 00:03:45.191
Directly underneath the connection,

53
00:03:45.191 --> 00:03:48.920
add db, setAttribute.

54
00:03:48.920 --> 00:03:51.415
Then within parentheses,
we add the attribute

55
00:03:51.415 --> 00:04:00.226
PDO::ATTR_ERRMODE followed
by the value we want to set,

56
00:04:00.226 --> 00:04:08.420
PDO error mode exception.

57
00:04:08.420 --> 00:04:13.130
This tells PDO that all errors
should be handled as an exception,

58
00:04:13.130 --> 00:04:16.490
which will then be caught
by a try-catch block.

59
00:04:16.490 --> 00:04:17.651
We'd save the changes and

60
00:04:17.651 --> 00:04:20.774
run the script again just to make
sure that we didn't break anything.

61
00:04:24.812 --> 00:04:26.674
Great, our connection is all set up and

62
00:04:26.674 --> 00:04:29.280
we're ready to start
interacting with our database.
