1 00:00:00,620 --> 00:00:04,510 We're going to start by getting a little more detail about our exceptions. 2 00:00:04,510 --> 00:00:08,710 When something goes wrong, we want to get as much information as possible. 3 00:00:08,710 --> 00:00:12,360 This allows us to quickly diagnose and to fix the problem. 4 00:00:12,360 --> 00:00:15,390 Let's open a browser window and search for the exception class. 5 00:00:16,600 --> 00:00:21,000 I'll open a new tab and search for exception php. 6 00:00:22,560 --> 00:00:25,550 And here we have the PHP Exceptions Manual. 7 00:00:25,550 --> 00:00:31,280 Click on extending exceptions, this shows us the actual exception class. 8 00:00:31,280 --> 00:00:35,900 If we scroll down, we'll see that we have several different methods. 9 00:00:35,900 --> 00:00:39,960 The method that we're going to want to use is, get message. 10 00:00:39,960 --> 00:00:43,620 This gives us a detailed message of what went wrong and 11 00:00:43,620 --> 00:00:46,310 returns that message as a string. 12 00:00:46,310 --> 00:00:47,750 So, let's head back over to our code. 13 00:00:48,780 --> 00:00:53,130 In our catch block, I'm going to add a new line after our echo statement. 14 00:00:53,130 --> 00:00:58,409 We're going to echo the actual exception objects message. 15 00:00:58,409 --> 00:01:03,240 Use dollar sign, e and then the object operator. 16 00:01:03,240 --> 00:01:09,060 That's a dash and a greater than sign, that together look like an arrow. 17 00:01:09,060 --> 00:01:10,980 We're telling it that we want to run a method. 18 00:01:12,720 --> 00:01:15,520 The method that we're going to run is getMessage. 19 00:01:16,580 --> 00:01:22,070 Remember, it's a method, like a function, so that means to call a function or 20 00:01:22,070 --> 00:01:26,130 a method, we need to use the opening and closing parentheses. 21 00:01:27,510 --> 00:01:30,823 We want to see an error, so let's break this again first. 22 00:01:33,244 --> 00:01:35,740 Add a couple more a's, and hit Save. 23 00:01:38,020 --> 00:01:39,494 Now let's go into our browser. 24 00:01:41,834 --> 00:01:44,802 So now it actually gives us an informative message, 25 00:01:44,802 --> 00:01:48,060 more than just, sorry I can't connect. 26 00:01:48,060 --> 00:01:50,300 Okay, so that tells us we can't open the file. 27 00:01:50,300 --> 00:01:53,080 Maybe we have a mistake in our connection string. 28 00:01:53,080 --> 00:01:55,710 So now that we know what to do, let's go back and fix that. 29 00:01:56,770 --> 00:01:59,230 We'll remove the extra A's, and hit Save. 30 00:02:00,680 --> 00:02:02,890 We'll just leave this for now, because it's a test project. 31 00:02:03,910 --> 00:02:06,830 Okay, the last thing we want to do is a command or 32 00:02:06,830 --> 00:02:08,589 method on the PDO object itself. 33 00:02:09,770 --> 00:02:14,180 We want any issues to throw an exception, instead of silencing them or 34 00:02:14,180 --> 00:02:16,250 just giving a simple warning. 35 00:02:16,250 --> 00:02:19,760 If there's an issue we want to know about it, so we can fix it quickly. 36 00:02:21,040 --> 00:02:23,260 This is what exception handling allows us to do. 37 00:02:24,720 --> 00:02:29,130 Let's go ahead and start a new search for the actual PHP PDO object. 38 00:02:29,130 --> 00:02:33,990 We'll search for PDO class, we'll click on the PDO from our manual. 39 00:02:35,840 --> 00:02:38,660 If you scroll down,you'll see that there are several different methods. 40 00:02:39,840 --> 00:02:44,560 The one that we want is setAttribute, so let's click here to find more information. 41 00:02:46,270 --> 00:02:49,520 So we say setAttribute and then here, you'll see that there 42 00:02:49,520 --> 00:02:54,140 are several constants or the actual attributes that you can change. 43 00:02:54,140 --> 00:02:57,190 We see attribute case and then attribute error mode. 44 00:02:58,380 --> 00:03:02,720 So, attribute error mode or error reporting is the one that we want. 45 00:03:02,720 --> 00:03:05,070 We want it to throw an exception no matter what. 46 00:03:06,310 --> 00:03:10,600 Let's go ahead and do that by calling set attribute on our actual PDO object. 47 00:03:12,000 --> 00:03:14,870 We're going to get rid of our var_dump since we don't need this anymore. 48 00:03:16,560 --> 00:03:21,921 We're then going to call our db with the object operator and 49 00:03:21,921 --> 00:03:27,750 then set attribute. 50 00:03:27,750 --> 00:03:30,380 We'll add our parenthesis and end our line. 51 00:03:30,380 --> 00:03:35,640 Let's go back to the browser and we'll copy this PDO attribute error mode. 52 00:03:37,370 --> 00:03:42,810 We paste this as our first argument, then we'll go back to the browser again and 53 00:03:42,810 --> 00:03:44,460 we'll copy this error mode exception. 54 00:03:47,150 --> 00:03:50,210 We'll paste this as our second attribute and then save our file. 55 00:03:51,280 --> 00:03:53,293 I'm going to preview this in the browser again, 56 00:03:53,293 --> 00:03:55,170 just to make sure we didn't break anything. 57 00:03:57,349 --> 00:03:59,509 Okay, we didn't do the var_dump, so 58 00:03:59,509 --> 00:04:03,110 we wouldn't see any of the details of our PDO object. 59 00:04:03,110 --> 00:04:05,260 But there is no error and no exception, 60 00:04:05,260 --> 00:04:08,680 nothing dying out, we just see our connection method. 61 00:04:08,680 --> 00:04:12,470 It seems like we're right where we want to be. 62 00:04:12,470 --> 00:04:15,068 [SOUND] We have a SQLite database for our catalog. 63 00:04:15,068 --> 00:04:19,669 We've written some code that connects to that database using an object of 64 00:04:19,669 --> 00:04:21,500 the PDO class. 65 00:04:21,500 --> 00:04:25,540 We've added a try catch statement to make sure that any exceptions 66 00:04:25,540 --> 00:04:29,600 that happen with our connection string will throw a descriptive error message, so 67 00:04:29,600 --> 00:04:33,380 that we can quickly diagnose and fix those issues. 68 00:04:33,380 --> 00:04:38,470 Finally, after creating the new PDO object, we set the error mode attribute 69 00:04:38,470 --> 00:04:43,450 of this object to throw an exception for any kind of error that happens. 70 00:04:43,450 --> 00:04:46,640 Then we can diagnose and fix those errors as well. 71 00:04:47,820 --> 00:04:49,820 We're off to a pretty good start. 72 00:04:49,820 --> 00:04:52,650 We're now ready to retrieve the data from the database and 73 00:04:52,650 --> 00:04:54,290 display it on the website. 74 00:04:54,290 --> 00:04:56,810 We'll tackle retrieving the data in the next section.