Handling Exceptions4:23 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 then 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 an external system 0:00 there's always the chance that something could go wrong. 0:03 What if the database is down? 0:06 How should your code respond? 0:07 These are known as exceptions and 0:10 any code connecting to an external system needs to handle them. 0:13 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:35 allow it to function as intended, then we want to catch that exception, and 0:39 most likely, log it, and notify someone in some way. 0:43 Go back into the working file connection.php. 0:48 Here we're creating a new pdo object that connects to our database. 0:51 Any code that connects to an external system should be placed inside a block of 0:55 code that can handle exceptions. 0:59 We can't always control external systems. 1:02 So it's a good idea to make sure we can still get the data we need. 1:05 In php the block of code for handling an exception is called the try catch block. 1:08 Before the code we want to try to execute, you put a try command. 1:13 You surround the code you want to try with a set of curly bracket. 1:17 Php will try to execute all the code inside those curly brackets. 1:23 You then put a catch command after the try block. 1:27 If an exception occurs php will execute the code inside this catch block. 1:32 After the catch, 1:37 you type a set of parenthesis with the following code inside. 1:38 Exception e. 1:43 When the exception occurs an exception will get passed to the catch block. 1:46 This is actually an object instantiating phps native exception class. 1:50 The way the exception gets passed to the catch block 1:56 is somewhat similar to how an argument gets passed to a function. 1:59 And this syntax with parentheses looks a bit like that. 2:02 Inside the catch block the variable e will contain the details about the exception. 2:06 After this we'll put another set of curly brackets. 2:12 If there is an exception the code inside these curly brackets will get executed. 2:16 After this let's just stop any more code from executing with the exit command. 2:27 Just to recap. 2:33 This block of code tries to create an object from the pto class. 2:34 Connected to our database. 2:38 If there's a problem, 2:40 an exception is thrown in the code inside a catch block is executed. 2:41 This message here is displayed and our code exits. 2:45 If everything connects just fine the catch block will be skipped. 2:49 And any code after this block would execute just fine. 2:52 Remember, the try catch block is not an if then statement. 2:56 We don't want the code in the catch block to ever be run. 3:00 Let's display a message down. 3:05 Now let's see what happens if the database is misspelled. 3:15 We'll add a couple extra A's, and save the file. 3:19 Now, we'll take a look at this in our browser. 3:21 Since the information needed to connect to the database is incorrect, 3:26 an exception is thrown. 3:30 The same thing would happen if the database server were offline. 3:31 The code in the catch block echoes out the message, and exits. 3:35 Let's go back and fix the connection string. 3:40 Now, in the browser you can see that the code to display the information about 3:47 the pdo object is executed from inside the try portion of our try catch block. 3:52 We also see the positive message after that block. 3:59 Because there is not an exception thrown, 4:02 the code inside the catch block is never run. 4:04 Before we continue, we're going to do two more things to our pdo object. 4:08 We want to make sure that we're aware of all issues and 4:12 make sure that those issues return as much useful information as possible. 4:16 This will help us fix those issues right away. 4:21
You need to sign up for Treehouse in order to download course files.Sign up