1 00:00:00,570 --> 00:00:03,570 Managing errors in any application is very important, 2 00:00:03,570 --> 00:00:06,110 especially when it comes to database errors. 3 00:00:06,110 --> 00:00:08,930 We will manage this by wrapping our code that communicates with 4 00:00:08,930 --> 00:00:11,150 the database within a try catch block. 5 00:00:12,350 --> 00:00:17,910 Continuing where we left off, we have created our PDO object successfully. 6 00:00:17,910 --> 00:00:21,090 And then I demonstrated how we can break that, so, we're gonna go ahead and 7 00:00:21,090 --> 00:00:22,170 break that again. 8 00:00:22,170 --> 00:00:24,390 So, I'm gonna add a few more A's back in and 9 00:00:25,450 --> 00:00:28,440 see how we can handle these errors and exceptions. 10 00:00:28,440 --> 00:00:30,470 Let's go ahead and refresh this. 11 00:00:30,470 --> 00:00:34,150 And now we'll see we have a fatal error, uncaught exception. 12 00:00:34,150 --> 00:00:35,420 Well, we don't want that to happen. 13 00:00:35,420 --> 00:00:39,000 And if you recall from object-oriented code practice. 14 00:00:39,000 --> 00:00:42,920 We have a lot of tri-catch statements when were' trying to catch exceptions and 15 00:00:42,920 --> 00:00:44,210 handle errors. 16 00:00:44,210 --> 00:00:47,660 So that's what we're gonna do, we're gonna handle this PDL exception, 17 00:00:47,660 --> 00:00:51,020 by catching any general exceptions in our script. 18 00:00:51,020 --> 00:00:54,650 So head back over to the index.php file. 19 00:00:54,650 --> 00:00:58,695 And then here we're gonna surround any connection to the database. 20 00:00:58,695 --> 00:01:01,742 So here where we're creating our new database, 21 00:01:01,742 --> 00:01:05,169 we're gonna actually surround that in a tri-block. 22 00:01:05,169 --> 00:01:09,660 So tri, and then open and close are curly braces. 23 00:01:10,680 --> 00:01:11,630 Then we have a catch. 24 00:01:13,690 --> 00:01:15,410 Open and close parens. 25 00:01:16,890 --> 00:01:18,440 And then open and close our curly braces. 26 00:01:18,440 --> 00:01:20,670 Now, this is try catch block. 27 00:01:20,670 --> 00:01:25,560 Inside of here, we're gonna try any code that we wanna run. 28 00:01:25,560 --> 00:01:30,170 And if it fails out or has an exception, we want to catch that exception. 29 00:01:30,170 --> 00:01:33,410 So here we just simply put the class that we want to catch, 30 00:01:33,410 --> 00:01:39,660 which is a PDO Exception, or we can use the parent class to catch any exception. 31 00:01:39,660 --> 00:01:41,760 So, we'll type in Exception with a capital E. 32 00:01:44,050 --> 00:01:49,230 And then the next argument is the actual exception object that gets 33 00:01:49,230 --> 00:01:51,390 thrown when the error happens. 34 00:01:51,390 --> 00:01:55,230 So that one we'll just call it dollar sign e for exception. 35 00:01:55,230 --> 00:01:57,940 Now we want to take this code here on line 11. 36 00:01:57,940 --> 00:02:00,200 In fact we'll do line 11, 12. 37 00:02:00,200 --> 00:02:02,300 We'll just copy it all. 38 00:02:03,390 --> 00:02:06,140 And we'll put it inside of our try block. 39 00:02:07,140 --> 00:02:09,860 So that way anything inside of 40 00:02:09,860 --> 00:02:15,830 here will happen only if the succesful database object is created. 41 00:02:15,830 --> 00:02:18,700 So the other half of this coin here or the other side of 42 00:02:18,700 --> 00:02:24,390 this coin is to actually put something in here for our exception when that happens. 43 00:02:24,390 --> 00:02:26,380 So right now I'm just gonna echo a string. 44 00:02:27,480 --> 00:02:35,280 In that string let's say, sorry, the connection was not successful. 45 00:02:37,370 --> 00:02:38,750 Okay? 46 00:02:38,750 --> 00:02:42,132 And then I also don't want it to continue so I'll add another die statement here. 47 00:02:42,132 --> 00:02:44,835 [BLANK_AUDIO] 48 00:02:44,835 --> 00:02:46,000 Close it with a semicolon. 49 00:02:46,000 --> 00:02:47,470 Save our file. 50 00:02:47,470 --> 00:02:49,100 And I'm gonna refresh. 51 00:02:49,100 --> 00:02:52,180 So instead of it throwing an uncalled exception, now it says, 52 00:02:52,180 --> 00:02:53,830 sorry the connection was not successful. 53 00:02:53,830 --> 00:02:58,300 Much cleaner, much nicer, no weird dumps to the screen about our code. 54 00:02:58,300 --> 00:02:59,310 Exactly what we want. 55 00:02:59,310 --> 00:03:05,000 Now to make sure it works, we're gonna go ahead and fix our error and then 56 00:03:07,000 --> 00:03:12,320 back in here, save our file, and then let's redo our preview. 57 00:03:13,650 --> 00:03:16,370 And now we have our PDO object. 58 00:03:16,370 --> 00:03:21,890 Before we continue on we do want to do 2 more things to our tri-catch block or 59 00:03:21,890 --> 00:03:23,770 to our PDO object and 60 00:03:23,770 --> 00:03:28,160 then our actual exception to make it a little bit more informative for us. 61 00:03:28,160 --> 00:03:33,960 So let's go ahead over to a new tab and let's open up the exception class. 62 00:03:33,960 --> 00:03:35,820 We're gonna go ahead and do that. 63 00:03:35,820 --> 00:03:38,730 I'm just gonna type in exception.php and 64 00:03:38,730 --> 00:03:44,660 here we have exception, which shows us the actual exception class. 65 00:03:44,660 --> 00:03:45,360 If we scroll down here, 66 00:03:45,360 --> 00:03:48,820 we'll see that we have several different methods that we can use. 67 00:03:48,820 --> 00:03:53,090 The one that we're going to want is get message, which if we click on 68 00:03:53,090 --> 00:03:58,020 get message information it'll just say it gets the exception message, and 69 00:03:58,020 --> 00:04:01,280 returns the exception message as a stray. 70 00:04:01,280 --> 00:04:05,100 Head back over to your code, here I'm going to modify line 11, 71 00:04:05,100 --> 00:04:08,010 I'm going to actually get rid of everything there just to echo. 72 00:04:09,080 --> 00:04:11,890 I'm gonna pass through the actual exception object. 73 00:04:11,890 --> 00:04:12,950 Just dollar sign e. 74 00:04:14,050 --> 00:04:16,010 And then an object operator. 75 00:04:16,010 --> 00:04:18,520 So that's a dash and a greater than symbol. 76 00:04:18,520 --> 00:04:20,520 Looks like an arrow pointing to the right. 77 00:04:20,520 --> 00:04:23,480 And that is how we tell it that we want to run a method. 78 00:04:23,480 --> 00:04:26,420 The method that we're gonna run is get message. 79 00:04:26,420 --> 00:04:29,290 So get and then capital m message. 80 00:04:31,400 --> 00:04:34,710 And remember it is a method, or also a function, so 81 00:04:34,710 --> 00:04:39,300 that means to call a function, we need to use the open and close parentheses. 82 00:04:39,300 --> 00:04:41,030 So we'll go ahead and do that. 83 00:04:41,030 --> 00:04:46,310 And then close it, save, and now let's refresh and see what the difference is. 84 00:04:47,800 --> 00:04:51,200 Okay, connection successful, so I'll go in here, break it again. 85 00:04:53,690 --> 00:04:56,920 Save our file, then refresh. 86 00:04:56,920 --> 00:05:00,730 And so now it actually gives us an informative message more than just sorry, 87 00:05:00,730 --> 00:05:01,850 I can't connect. 88 00:05:01,850 --> 00:05:06,952 It actually tells you SQL state hey, we're unable to open the database file. 89 00:05:06,952 --> 00:05:10,170 All right, so that tells us we can open the file may be we did a mistake in 90 00:05:10,170 --> 00:05:13,810 our connection string, which now we know we did, so I'll go and 91 00:05:13,810 --> 00:05:16,690 close that out and then hit Save. 92 00:05:16,690 --> 00:05:22,140 Okay, the final step, the last thing that we want to do, is a command on the actual 93 00:05:22,140 --> 00:05:26,770 PDO object itself or a method on the PDO object itself. 94 00:05:26,770 --> 00:05:28,340 So we need to go ahead and 95 00:05:28,340 --> 00:05:33,560 open up a new tab to search for the actual PHP PDO object. 96 00:05:33,560 --> 00:05:36,490 So we'll do PDO class. 97 00:05:36,490 --> 00:05:40,030 Then the PHP PDO class is in the manual. 98 00:05:40,030 --> 00:05:42,720 Okay and if you scroll down here you'll see that 99 00:05:42,720 --> 00:05:45,390 there are several different methods. 100 00:05:45,390 --> 00:05:49,210 The one that we're interested in is setattribute. 101 00:05:49,210 --> 00:05:51,510 So we say, set attribute. 102 00:05:51,510 --> 00:05:54,720 And then in here, you'll see there are several constants or 103 00:05:54,720 --> 00:05:57,050 the actual attributes that you can change. 104 00:05:57,050 --> 00:05:58,020 That's here in the header. 105 00:05:58,020 --> 00:06:02,750 So you'll see attribute case, and then attribute error mode. 106 00:06:02,750 --> 00:06:08,290 So attribute error mode or the error reporting is what we want to modify. 107 00:06:08,290 --> 00:06:10,780 So we wanna, scroll down a little bit more here. 108 00:06:10,780 --> 00:06:14,130 We wanna modify this so that the error mode, when that happens, 109 00:06:14,130 --> 00:06:19,930 anything that happens at all instead of silencing them or having a little warning. 110 00:06:19,930 --> 00:06:22,540 We actually wanna set it to throw exception. 111 00:06:22,540 --> 00:06:26,810 So no matter what the error is, we're gonna throw an exception. 112 00:06:26,810 --> 00:06:30,010 So let's go ahead and do that by calling set 113 00:06:30,010 --> 00:06:34,820 attribute on our actual PDO object, or the dollar sign DB. 114 00:06:34,820 --> 00:06:41,890 So I'll head back over to our index.php file and we'll do it directly underneath. 115 00:06:41,890 --> 00:06:45,920 So we're gonna get rid of this, and we don't need this guy anymore. 116 00:06:47,680 --> 00:06:56,832 And then here, we're gonna do $db, object operator to setAttribute. 117 00:06:56,832 --> 00:06:59,220 All right, and then close that line with a semicolon. 118 00:06:59,220 --> 00:07:02,780 Now if you go back to where we were in the manual. 119 00:07:02,780 --> 00:07:05,980 You'll see that we have the attribute and then the value. 120 00:07:05,980 --> 00:07:10,310 So, our attribute that we're gonna set, copy this constant here, 121 00:07:10,310 --> 00:07:14,320 and we're gonna paste that in. 122 00:07:14,320 --> 00:07:15,380 That's our first argument. 123 00:07:15,380 --> 00:07:17,520 Then we're gonna add a comma and 124 00:07:17,520 --> 00:07:21,400 our second argument is the mode that we want to set it to. 125 00:07:21,400 --> 00:07:26,058 And that would be PDO error mode exception. 126 00:07:26,058 --> 00:07:28,970 Copy that paste it in. 127 00:07:28,970 --> 00:07:33,460 Hit Save, and then I'm gonna do my preview just to make sure it's all good. 128 00:07:34,610 --> 00:07:38,150 Okay, we didn't do the var dump, so we wouldn't see anything different, but 129 00:07:38,150 --> 00:07:41,260 there's no errors, no exceptions, nothing dying out. 130 00:07:41,260 --> 00:07:43,700 It seems like we're right where we wanna be. 131 00:07:43,700 --> 00:07:46,260 Let's go ahead and close this guy out. 132 00:07:46,260 --> 00:07:48,290 And then go back and review here. 133 00:07:48,290 --> 00:07:52,670 So we've added a try catch statement to make sure there are any exceptions that 134 00:07:52,670 --> 00:07:58,130 happen on the PDO connection string, will throw an error message to us and 135 00:07:58,130 --> 00:07:59,830 then die out the script. 136 00:07:59,830 --> 00:08:03,450 And then we also after creating the new PDO object, 137 00:08:03,450 --> 00:08:07,440 we have sat the error mode attribute of the PDO object or 138 00:08:07,440 --> 00:08:13,530 the DB object, to throw all exceptions for any kind of error that happens. 139 00:08:13,530 --> 00:08:17,430 Next up we wanna run some queries and see what films are in our database.