Managing Errors8:17 with Hampton Paulk
Managing errors in any application is very important, especially when it comes to database errors. We will manage this by wrapping our code that communicates with the database with a try catch block.
Managing errors in any application is very important, 0:00 especially when it comes to database errors. 0:03 We will manage this by wrapping our code that communicates with 0:06 the database within a try catch block. 0:08 Continuing where we left off, we have created our PDO object successfully. 0:12 And then I demonstrated how we can break that, so, we're gonna go ahead and 0:17 break that again. 0:21 So, I'm gonna add a few more A's back in and 0:22 see how we can handle these errors and exceptions. 0:25 Let's go ahead and refresh this. 0:28 And now we'll see we have a fatal error, uncaught exception. 0:30 Well, we don't want that to happen. 0:34 And if you recall from object-oriented code practice. 0:35 We have a lot of tri-catch statements when were' trying to catch exceptions and 0:39 handle errors. 0:42 So that's what we're gonna do, we're gonna handle this PDL exception, 0:44 by catching any general exceptions in our script. 0:47 So head back over to the index.php file. 0:51 And then here we're gonna surround any connection to the database. 0:54 So here where we're creating our new database, 0:58 we're gonna actually surround that in a tri-block. 1:01 So tri, and then open and close are curly braces. 1:05 Then we have a catch. 1:10 Open and close parens. 1:13 And then open and close our curly braces. 1:16 Now, this is try catch block. 1:18 Inside of here, we're gonna try any code that we wanna run. 1:20 And if it fails out or has an exception, we want to catch that exception. 1:25 So here we just simply put the class that we want to catch, 1:30 which is a PDO Exception, or we can use the parent class to catch any exception. 1:33 So, we'll type in Exception with a capital E. 1:39 And then the next argument is the actual exception object that gets 1:44 thrown when the error happens. 1:49 So that one we'll just call it dollar sign e for exception. 1:51 Now we want to take this code here on line 11. 1:55 In fact we'll do line 11, 12. 1:57 We'll just copy it all. 2:00 And we'll put it inside of our try block. 2:03 So that way anything inside of 2:07 here will happen only if the succesful database object is created. 2:09 So the other half of this coin here or the other side of 2:15 this coin is to actually put something in here for our exception when that happens. 2:18 So right now I'm just gonna echo a string. 2:24 In that string let's say, sorry, the connection was not successful. 2:27 Okay? 2:37 And then I also don't want it to continue so I'll add another die statement here. 2:38 [BLANK_AUDIO] 2:42 Close it with a semicolon. 2:44 Save our file. 2:46 And I'm gonna refresh. 2:47 So instead of it throwing an uncalled exception, now it says, 2:49 sorry the connection was not successful. 2:52 Much cleaner, much nicer, no weird dumps to the screen about our code. 2:53 Exactly what we want. 2:58 Now to make sure it works, we're gonna go ahead and fix our error and then 2:59 back in here, save our file, and then let's redo our preview. 3:07 And now we have our PDO object. 3:13 Before we continue on we do want to do 2 more things to our tri-catch block or 3:16 to our PDO object and 3:21 then our actual exception to make it a little bit more informative for us. 3:23 So let's go ahead over to a new tab and let's open up the exception class. 3:28 We're gonna go ahead and do that. 3:33 I'm just gonna type in exception.php and 3:35 here we have exception, which shows us the actual exception class. 3:38 If we scroll down here, 3:44 we'll see that we have several different methods that we can use. 3:45 The one that we're going to want is get message, which if we click on 3:48 get message information it'll just say it gets the exception message, and 3:53 returns the exception message as a stray. 3:58 Head back over to your code, here I'm going to modify line 11, 4:01 I'm going to actually get rid of everything there just to echo. 4:05 I'm gonna pass through the actual exception object. 4:09 Just dollar sign e. 4:11 And then an object operator. 4:14 So that's a dash and a greater than symbol. 4:16 Looks like an arrow pointing to the right. 4:18 And that is how we tell it that we want to run a method. 4:20 The method that we're gonna run is get message. 4:23 So get and then capital m message. 4:26 And remember it is a method, or also a function, so 4:31 that means to call a function, we need to use the open and close parentheses. 4:34 So we'll go ahead and do that. 4:39 And then close it, save, and now let's refresh and see what the difference is. 4:41 Okay, connection successful, so I'll go in here, break it again. 4:47 Save our file, then refresh. 4:53 And so now it actually gives us an informative message more than just sorry, 4:56 I can't connect. 5:00 It actually tells you SQL state hey, we're unable to open the database file. 5:01 All right, so that tells us we can open the file may be we did a mistake in 5:06 our connection string, which now we know we did, so I'll go and 5:10 close that out and then hit Save. 5:13 Okay, the final step, the last thing that we want to do, is a command on the actual 5:16 PDO object itself or a method on the PDO object itself. 5:22 So we need to go ahead and 5:26 open up a new tab to search for the actual PHP PDO object. 5:28 So we'll do PDO class. 5:33 Then the PHP PDO class is in the manual. 5:36 Okay and if you scroll down here you'll see that 5:40 there are several different methods. 5:42 The one that we're interested in is setattribute. 5:45 So we say, set attribute. 5:49 And then in here, you'll see there are several constants or 5:51 the actual attributes that you can change. 5:54 That's here in the header. 5:57 So you'll see attribute case, and then attribute error mode. 5:58 So attribute error mode or the error reporting is what we want to modify. 6:02 So we wanna, scroll down a little bit more here. 6:08 We wanna modify this so that the error mode, when that happens, 6:10 anything that happens at all instead of silencing them or having a little warning. 6:14 We actually wanna set it to throw exception. 6:19 So no matter what the error is, we're gonna throw an exception. 6:22 So let's go ahead and do that by calling set 6:26 attribute on our actual PDO object, or the dollar sign DB. 6:30 So I'll head back over to our index.php file and we'll do it directly underneath. 6:34 So we're gonna get rid of this, and we don't need this guy anymore. 6:41 And then here, we're gonna do $db, object operator to setAttribute. 6:47 All right, and then close that line with a semicolon. 6:56 Now if you go back to where we were in the manual. 6:59 You'll see that we have the attribute and then the value. 7:02 So, our attribute that we're gonna set, copy this constant here, 7:05 and we're gonna paste that in. 7:10 That's our first argument. 7:14 Then we're gonna add a comma and 7:15 our second argument is the mode that we want to set it to. 7:17 And that would be PDO error mode exception. 7:21 Copy that paste it in. 7:26 Hit Save, and then I'm gonna do my preview just to make sure it's all good. 7:28 Okay, we didn't do the var dump, so we wouldn't see anything different, but 7:34 there's no errors, no exceptions, nothing dying out. 7:38 It seems like we're right where we wanna be. 7:41 Let's go ahead and close this guy out. 7:43 And then go back and review here. 7:46 So we've added a try catch statement to make sure there are any exceptions that 7:48 happen on the PDO connection string, will throw an error message to us and 7:52 then die out the script. 7:58 And then we also after creating the new PDO object, 7:59 we have sat the error mode attribute of the PDO object or 8:03 the DB object, to throw all exceptions for any kind of error that happens. 8:07 Next up we wanna run some queries and see what films are in our database. 8:13
You need to sign up for Treehouse in order to download course files.Sign up