 Hey everybody, this is Brian. Welcome to the 44th tutorial on LAMP. We are going to be discussing object-oriented programming error handling. If you remember our conversation when we were talking about the error methods in PHP, well, those were for PHP 4. PHP 5 added a lot of object-oriented programming. Well, you have to handle errors differently. If you go out to W3 schools, they talk about exemptions. Well, it says throw new exemption. Well, what does that mean? We don't really know what throw means, but new exemption. That looks a lot like object-oriented programming and you'd be right. The exemption is a class built into PHP, but they don't explain anything about object-oriented programming. They just take for granted that you just magically know from here to here. Well, that's like the last dozen or so videos that we've just done is nothing but object-oriented programming. So, we know now everything we need to know to understand all of this. We need to understand what does throw do? Well, let's write some code. Class H check. And we're just going to say public function check age. And we're going to say age. If the age is greater than zero then else. Otherwise, what we're going to do here is we're going to throw an error. This is a bad situation. Something we don't want in our code, meaning the age is now zero. If we've gotten this far, we're saying age. If age is greater than zero, so age is not greater than zero, it ends up down here. Well, we want to do something. Now, typically we would just echo something out, but we're in object-oriented land. And let's say we're writing this class and somebody else is going to use it. So, what we're going to do here is we're going to throw a new exemption. Age cannot be zero. So, now let's just make an instance of this class. We're just going to say A equal new. Age check. We're going to check the age. And let's give it the ripe old age of 99. You are 99. I should probably put years old. A bit of a perfectionist in me coming out. Let's say we give it a zero. Notice how nothing happens. Let's actually echo something out here. No. That way we know something happened. Run it again. Still nothing happens. Why is that? Well, when you throw an exemption, what you're doing is you're taking this and making it into an error. You're saying an error occurred. Stop execution, stop everything. I mean, if we comment that out, we can see our algorithm works, but the throw actually stops execution. So, when you hit an error, execution will stop at the first time something is thrown. Let's make another class. And we'll say person. Public function set age. We're just going to set the age. And we need the age check class. So, we're going to say, actually let's give it the age variable. We're just going to say A. That way you don't confuse it. Notice how, well, we haven't done that right. Let's actually finish this up. Let's change A to a person. And we're going to set age. Notice how still it doesn't work. We're making a new person class. We're calling set age. Set age is just making a new age check class. I should say age check object. And then we're calling this check age. So, we're still in the same situation where this is not being displayed. So, we have no idea what's going on. What we need to do is catch the error. Think of this like throwing a ball. We're throwing the exception. Now we need to catch it. So, let's say try and then catch. And this is one of the few times where you will actually declare a type. So, here is our try and catch. We have two code blocks. Try, catch. And what we're saying here is anything within the try code block, try to do it. So, we're going to take our code, cut it, paste it. And here's our catch. Now, we have the catch and we have a special type called an exception. If you go out to the manual you'll see that the exception is just a class within PHP. And it has a lot of different things like getMessage, previous, code, file, line, trace, trace of string. Some of these will cover like message. It just gets a message saying, hey, what is the message of the exception? File tells you the file name line tells you the line. Trace is an interesting one. It traces back through the whole context of execution. So, let's actually demonstrate these a little bit. Let's actually take this, paste it here. Now we see, ta-da, we've caught the exception. No! But we need to extract some information out of here. So, let's actually say age cannot be zero. So, now we're actually getting the information out of the exception class through the getMessage function. So, we know that something happened. And this is pretty typical in just about any language. Someone will write this class, some condition happens, and they will throw an exception. Then it's up to you to catch the exception. Meaning, you work with their class and then if an error occurs you catch it and handle it. Never, ever, ever, ever, ever do this. Because then an error occurred and you have no idea what happened. So, now that we have gotten the message, we can do something else like get trace as string. Ta-da! Look at that! Error number zero and it actually gives us the full path of the file. The line age check, check age zero. It actually gives us the code that we've called out. Error number one. So, on tut 44 PHP line 48 person set age zero. Which, sure enough, line 48. There's where it's generating. And then it drills it back. It's called a trace string. It traces it all the way back to the very beginning of where this happened. In age check, check age zero. Age check, check age zero. Ta-da! So, that's how that works. Very, very powerful. Heavily used across all object-oriented languages. And we can actually take this a step further and say class my exception. Let's actually grab this for copy and play. Let's just call it my error. Try to keep things short. And then let's say public function doom. I like doom. So, let's say, notice how we're returning a string. And then we can just say the sky is falling in. Ah, yes, I see what the problem here is. We have to actually, sorry, extend the exception object. And now, suddenly, we have things like get file. Where we can get the actual file name at. So, we haven't actually used this class yet. So, here's our my error class. Where we're extending the exception object. And we have a doom function. So, we're just doing some sort of custom error handling here. So, what we can do here is actually let's change this around a little bit. And I'm going to just comment this out rather than delete it. That way, if you guys download the source code off my website, you can see it later. Now, notice how nothing's changed. It's because we're still doing the e get trace. But if we say doom, the sky is falling in and there's our file and at line 34. So, that is how you would actually do a custom error class. So, let's review it real quick. I realize I threw a lot at you very quickly. We have a class called age check, which has a function called check age. We're just saying if the age is greater than zero, you're all good. Otherwise, something bad happened. You're going to throw an exemption and then in your consumer class, you would have to catch the exemption by using try catch. Now, you very easily could have put try and catch right here. But we just want to demonstrate how to use this across classes. So, now we're calling the function. We're trying that code. If something is thrown, we're catching it. Notice how we're catching the exemption. But we still call doom. We should actually do this. And then you can actually nestle it down again. That way you can catch multiple levels of errors. I hope if I actually spelled this right, get trace as string. So, what we're saying here is there's one of two ways we can do this. We can catch our custom error or we can catch an exemption. Now, our custom exemption actually extends exemption. You would do this just in case, let's say instead of my error getting thrown, you would throw an actual exemption. Notice how you have two catches. This is called fall through. We're catching our custom error type. This is doing, if you watched my previous video about type checking, this is basically saying, is this the my error class? That's why you're giving it the actual type. It's saying no it's not, so go to the next catch. And then our catch all is this catch exemption error. So, it's giving the same thing. So, if we comment this out and we use this one instead, we get different functionality. That and that shell is exemption handling inside of object-oriented programming. Hope you found this educational and entertaining and thank you for watching.