Handling Exceptions4:30 with Alena Holligan
What happens when the system is unable to connect to the database? Whenever your code relies on an external system, there's always the chance that something could go wrong. These are known as exceptions and any code connecting to an external system needs to handle them.
Although exception handling works in both PHP 5 and PHP 7, they will display slightly different exceptions. This means that the exact error text displayed could be different than what you see in the video, depending on which version of PHP you are running. This does not affect functionality and you should be able to follow along in any version of PHP 5 or above. PHP 7 introduced more precise exceptions. Instead of just throwing a generic exception, PHP 7 will also specify if this is an ArithmeticError, DivisionByZeroError, AssertionError, ParseError, or TypeError.
To learn more about the changes in PHP 7, check out our Introduction to PHP 7 Workshop
Whenever your code relies on its external system, 0:00 there's always the chance that something could go wrong. 0:03 What if the database is down, how should your code respond? 0:06 These are known as exceptions and 0:10 any code connecting to an external system needs to handle them. 0:12 Managing errors in any application is very important, 0:17 especially when it comes to database errors. 0:21 Remember, exceptions are exceptional, 0:24 you shouldn't use them with code that you're expecting to happen. 0:27 If the code is functioning as designed, it should never hit an exception. 0:31 However, if the code encounters something that will not 0:36 allow it to function as intended, then we want to catch an exception and 0:39 most likely log it and notify someone in some way. 0:44 Any code that connects to an external system should 0:48 be placed inside a block of code that can handle exceptions. 0:51 In PHP the block of code for handling exceptions is called a try-catch block. 0:55 Let's go back to workspaces and add that to our code. 1:01 Before the code you want to try to execute, you put a try command. 1:05 You surround the code you want to try in a set of curly braces. 1:11 This tells PHP to try to execute all the code inside those curly braces. 1:17 You then put a catch command after the try block. 1:23 When the exception occurs, an exception will get passed to the catch block. 1:26 After catch you type a set of parentheses with the following code inside them 1:32 (Exception $e), which 1:36 will catch the exception that occurred and put the details into the variable e. 1:41 Finally, you put another set of curly braces. 1:47 If there is an exception, the code inside these curly braces will get executed. 1:50 If our code above inside the try block cannot connect to the database, 1:56 we can display a detailed message using $e->getMessage. 2:01 Then stop any more code from executing with the exit command. 2:10 Just to recap, this block of code tries to create an object from the PDO 2:15 class connecting to our database. 2:20 If there is a problem, an exception is thrown and 2:22 the code inside our catch block is executed. 2:25 This message here is displayed and our code ends. 2:28 If the connection is successful, the catch block would be skipped and 2:32 any code after this block would continue to be executed. 2:36 Remember, the try-catch block is not an if-then statement. 2:39 We do not want the code in our catch block to run. 2:45 Let's see what happens when we run the script in the console now. 2:49 Since the information needed to connect to the database is incorrect, 2:53 an exception is thrown. 2:57 The same thing would happen if the database server were offline. 2:59 The code in the catch block echoes out this message and exits. 3:02 Let's go back and fix the connection string and run the script again. 3:07 Because there was no exception thrown, the code inside the catch block is never run. 3:16 And the code is executed to var_dump, this newly created database object. 3:21 No matter what error occurs, I want PDO to throw an exception so 3:26 we can catch it and fix it. 3:30 So let's go ahead and 3:32 do that by calling set attribute on our new PDO object, the variable db. 3:33 Directly underneath the connection, 3:39 add db, setAttribute. 3:45 Then within parentheses, we add the attribute 3:48 PDO::ATTR_ERRMODE followed by the value we want to set, 3:51 PDO error mode exception. 4:00 This tells PDO that all errors should be handled as an exception, 4:08 which will then be caught by a try-catch block. 4:13 We'd save the changes and 4:16 run the script again just to make sure that we didn't break anything. 4:17 Great, our connection is all set up and 4:24 we're ready to start interacting with our database. 4:26
You need to sign up for Treehouse in order to download course files.Sign up