More Exceptions4:57 with Alena Holligan
In this video we’ll show how to get the details of the exception so we can quickly diagnose and fix the problem. We also want to tell the PDO object to make any issue an exception so we can handle them the same way.
Calling Properties and Methods
We use the single arrow (->) to call both an objects properties as well as an objects methods. Methods should end with parenthesis, just like functions, with or without passing an argument.
Extending Exceptions will allow you to customize what happens when an exception is encountered. You can modify the message displayed, alert support or anything else you would do in your PHP code. Check out the Documentation for Extending Exceptions.
We're going to start by getting a little more detail about our exceptions. 0:00 When something goes wrong, we want to get as much information as possible. 0:04 This allows us to quickly diagnose and to fix the problem. 0:08 Let's open a browser window and search for the exception class. 0:12 I'll open a new tab and search for exception php. 0:16 And here we have the PHP Exceptions Manual. 0:22 Click on extending exceptions, this shows us the actual exception class. 0:25 If we scroll down, we'll see that we have several different methods. 0:31 The method that we're going to want to use is, get message. 0:35 This gives us a detailed message of what went wrong and 0:39 returns that message as a string. 0:43 So, let's head back over to our code. 0:46 In our catch block, I'm going to add a new line after our echo statement. 0:48 We're going to echo the actual exception objects message. 0:53 Use dollar sign, e and then the object operator. 0:58 That's a dash and a greater than sign, that together look like an arrow. 1:03 We're telling it that we want to run a method. 1:09 The method that we're going to run is getMessage. 1:12 Remember, it's a method, like a function, so that means to call a function or 1:16 a method, we need to use the opening and closing parentheses. 1:22 We want to see an error, so let's break this again first. 1:27 Add a couple more a's, and hit Save. 1:33 Now let's go into our browser. 1:38 So now it actually gives us an informative message, 1:41 more than just, sorry I can't connect. 1:44 Okay, so that tells us we can't open the file. 1:48 Maybe we have a mistake in our connection string. 1:50 So now that we know what to do, let's go back and fix that. 1:53 We'll remove the extra A's, and hit Save. 1:56 We'll just leave this for now, because it's a test project. 2:00 Okay, the last thing we want to do is a command or 2:03 method on the PDO object itself. 2:06 We want any issues to throw an exception, instead of silencing them or 2:09 just giving a simple warning. 2:14 If there's an issue we want to know about it, so we can fix it quickly. 2:16 This is what exception handling allows us to do. 2:21 Let's go ahead and start a new search for the actual PHP PDO object. 2:24 We'll search for PDO class, we'll click on the PDO from our manual. 2:29 If you scroll down,you'll see that there are several different methods. 2:35 The one that we want is setAttribute, so let's click here to find more information. 2:39 So we say setAttribute and then here, you'll see that there 2:46 are several constants or the actual attributes that you can change. 2:49 We see attribute case and then attribute error mode. 2:54 So, attribute error mode or error reporting is the one that we want. 2:58 We want it to throw an exception no matter what. 3:02 Let's go ahead and do that by calling set attribute on our actual PDO object. 3:06 We're going to get rid of our var_dump since we don't need this anymore. 3:12 We're then going to call our db with the object operator and 3:16 then set attribute. 3:21 We'll add our parenthesis and end our line. 3:27 Let's go back to the browser and we'll copy this PDO attribute error mode. 3:30 We paste this as our first argument, then we'll go back to the browser again and 3:37 we'll copy this error mode exception. 3:42 We'll paste this as our second attribute and then save our file. 3:47 I'm going to preview this in the browser again, 3:51 just to make sure we didn't break anything. 3:53 Okay, we didn't do the var_dump, so 3:57 we wouldn't see any of the details of our PDO object. 3:59 But there is no error and no exception, 4:03 nothing dying out, we just see our connection method. 4:05 It seems like we're right where we want to be. 4:08 [SOUND] We have a SQLite database for our catalog. 4:12 We've written some code that connects to that database using an object of 4:15 the PDO class. 4:19 We've added a try catch statement to make sure that any exceptions 4:21 that happen with our connection string will throw a descriptive error message, so 4:25 that we can quickly diagnose and fix those issues. 4:29 Finally, after creating the new PDO object, we set the error mode attribute 4:33 of this object to throw an exception for any kind of error that happens. 4:38 Then we can diagnose and fix those errors as well. 4:43 We're off to a pretty good start. 4:47 We're now ready to retrieve the data from the database and 4:49 display it on the website. 4:52 We'll tackle retrieving the data in the next section. 4:54
You need to sign up for Treehouse in order to download course files.Sign up