 Hey everybody, this is Brian. Welcome to Video 103. This is cute with C++ and GUI programming. And as you can see, we have my beautiful website, voidrealms.com out there. You can find the complete video tutorial plus source code at no charge out on my website. So before you bombard me with emails, go to voidrealms.com and get it. I know it's been a while since I've done a video, so I kind of feel like I owe you guys a little bit of an explanation as to where I have been. As you can see, there's me on one of my happier days. I love that shirt. There's no place like 1-2-7-0-0-1. No place like home. A couple of you nerds out there chuckled. Everybody else was like, huh? Anyways, you can see I have all these certifications by my name. I'm actually a programmer along with being a network administrator and I have quite a few years of experience by my name. I got a new job. I am now management. I'm this guy. Gunnery Sergeant. I don't remember his name or full metal jacket. Anyways, I've got about two teams underneath me and I'm learning the management ropes and it is quite challenging. And on top of that, I have to get a Cisco CCNA certification within six months. I've never touched a router or switch at that level before, so this is proving to be quite a challenge for me. On top of new job, kid, girlfriend, cats, which are currently fighting on top of my wireless router, get out of there. So I wanted to discuss something today, which was actually a viewer request. I think it was Jackal96. I'm sorry, it's been a long day. I don't remember if it was Jackal or Joker, but something96 said, hey, can you cover C++ exemptions? And I thought that's the dumbest thing in the world. And then I thought about it and said, we've never covered this. I wonder how many other people are wondering this too. And then it dawned on me that Qt really doesn't cover exemptions. Qt goes through like a Boolean model, like is the file open true or false? Did you copy the file? True or false? But it doesn't really throw exemptions, not like the standard library does. So if you go out to c++.com reference and look up exemption, you'll see there is a C++ exemption class in the standard library. And this is what it looks like. It's just a standard class. It has, you know, the ability to throw. You have an exemption object in there and you have a character string of what, which is just a basic description of what the heck just happened. And they have a very simple little, you know, example here. You can see there's IO stream, type info, namespace standard, and you're just creating a polymorphic class with a virtual function. Kitty, if you knock over my router, I'm going to be unhappy with you. Sorry guys. And then we're just, you know, creating an object. And this is where we're actually throwing the exemption. We're actually what we're doing here is we're creating an exemption intentionally. And then we're catching it through a try catch mechanism. And I realized for a lot of you out there, this is old school. You're like, yeah, yeah, I know, why are you even covering this? Well, we've never covered it in Qt because Qt doesn't really support exemptions. It's not really built into the Qt framework. I'm sorry, the Qt model. So we should cover it. And the reason why is simple. That is my shower. Or let me explain better. That is what was underneath my shower. See, programs are a lot like houses. You need a solid foundation to build upon because if you don't, things fall apart rather quickly. And I'm not going to bore you with the details, but I spent a good two weeks fixing my shower. I had to rip the whole thing out. Let me back up here. Yes, that's PVC pipe and this wood thing. I mean, this whole shower was supported by a 2x4. There's the shower drain. What happened is I got into my shower one day and my foot went through the bottom and I'm like, oh, great. Well, I found out why. There's no foundation. You can see the wood's totally rotting. And you can see there's some mold. And here's a little close-up. Gosh, I was so pissed off. I called that contractor and chewed them out. But this is what happens when you don't do the job right the first time. You can see there's mold, water damage. And this has been going on since I remodeled my bathroom four years ago. So I ended up ripping the entire floor out, putting new floor joists in, putting a new floor in. Here's another shot of the floor. This is what was holding up the entire shower. Now I'm showing you these, not so you can listen to me gripe and won't want to bop my shower, but this actually mimics how a lot of software developers will build their programs. They build a shoddy foundation. The entire thing's propped up by one chintzy little slap-together hack. And then they just hope it works. And this is me doing it right, rebuilding the entire thing. Did all this. My girlfriend helped me. And we just did it right. Got the walls up and everything. But point being, if you don't build it right the first time, you're going to suffer. And I'm sure, being in Qt, you guys have probably worked with this enough to know that you mess up, you throw errors. So we're going to make a console application. We're going to call this Errors. Put it in the usual location. And I have to apologize if my typing is kind of slow. I actually have not done a tutorial in what feels like forever. And I'm really dying to get back into doing tutorials. Hmm, the following over exists. Let me pick a different name. Sorry about that. New project, console application. What should we call this? Example error. Yeah, my day has been just so chaotic. I mean, for example, I had an argument with the janitor of all things. The janitor. He was all mad because I didn't recycle correctly. So he dumped a bunch of trash on my desk. And I'm like, really, dude? Trash. Come on. All right. Big fan of copy and paste. So right off the bat, we're just going to save a few seconds of time here and put in Qt bug and Q string. We've got just our standard application here. And we're going to make a test function. And then we're just going to call that test function. No black magic here. And we're just going to say Qt bug. Now Qt bug is something I want to cover in future tutorials. Qt bug is invaluable as a debugging tool when you get into graphic programming. You can actually have a console window spitting out Qt bug messages while your GUI is running. And you can turn that on and off at will. It's actually pretty slick. Okay. I'll say hello world. Get out of here, cat. Sorry, my cat thinks it's playtime all of a sudden. So just real simple. Let's give this a good build and run. Tick tock, tick tock. There we go. Very simple, very easy application. You've seen this a million times. Now, like the shower, something could horribly, horribly go wrong. For example, let's just throw an error. This is called throwing an error in case you haven't seen this. We'll say no. Now, what throw does, in case you've never heard of it, is it throws an error, meaning it creates an error. You're intentionally causing a problem. Kind of like the contractor did to my shower. Sorry, I'm still kind of mad about that. And this is what happens, and you've probably seen this in your programs before when something goes very, very wrong. Debug error, blah, blah, blah, blah. And it says, do you want to abort, retry, or ignore? And if you have specific SDKs or programming tools, it'll say, do you want to actually go in and debug the application? That's something we're not going to cover in this tutorial. It's a little advanced for right now, but we will get there. For right now, we're just going to abort. The problem is, your program just stopped. So if you're in the middle of running, like, I don't know, your taxes or saving the world, curing cancer, staving off a zombie apocalypse, well, all that just came to an abrupt end. Then you have to restart your program all over again. That can get very, very bothersome. So what we have is what's called a try catch mechanism. Try means anything in this block we're going to attempt. Catch, anything in this block is where we're going to catch it. Think of this like baseball. Try is going to throw it out there. Throw is probably a bad word. It's going to pitch it out there. And then we're going to catch it. Now, if an error is thrown, for example, we're going to raise that error, we're going to catch it here. Think of this as, think of this code block as a mini function in itself. This is where we're going to catch the error. We're just going to say Q to bug. And we're going to give the, what will now be known in infamy as the windows catch all unknown error has occurred. I'm sure you guys have seen that a million times. So let's actually get that out of there. So very standard, very simple. Now you kind of, if you're new to error handling, now you're kind of going, ah, I understand what happens now whenever I run windows 95 and it pops up and says unknown error. That's exactly what's going on. Something happens and the developers did what's called a catch all. That's what these three little ellipses are. A catch all, meaning anything is going to drop into here. I should say any unhandled exemption. You'll see what I mean in a minute. No, no, kitty, don't bite the USB. That's bad. These cats are just all over my desk because I haven't been here. Now you see, same program, but it says unknown error has occurred and it's still running. So what we could have done is hello world. And let's just say glad. Help effects bug glad. That's over. And let's actually change hello world to starting. So one thing I am thinking about doing kind of off topic here is building some CCNA towards torts. I'm sorry, tutorials. Alright, there we go. As you can see, starting unknown error has occurred. Glad it's over. So we can actually resume processing here. Now, why would that be handy? Well, unknown error in itself isn't very handy at all. Notice how we're throwing out this nose. Let's say the cat ate my homework. Because my cats sometimes do actually eat my homework. And I really can't spell today. The cat ate my homework. Now when we run this, what do you think is going to happen? Look at the code real quick while it's building. What's it going to print out? If you said the cat ate my homework, I'm sorry. You did not win. It still says unknown error has occurred. That is because of this catch all. That catch all right there is just going to say, I give up. I don't know what the problem is. Just do something. So what we need to do is actually handle the error. So if you think this is a Q string because we're in cute, well let's give that a try and see what happens. I always hate books that do this, but I'm going to do it. Just for illustrative purposes. I had this one C++ book that I absolutely hated. It was like the first book I ever read. And what it said was, okay, type in all this code. At the end of typing in all this code, and I wasn't a very fast typer by any means at that time. So it took me a good hour and a half. Type it all in. The author says on the next page, of course, is, haha, that didn't work. How stupid do you feel? I mean, that's literally what the book said. As you can see, we are catching a Q string here, meaning this is a Q string and we're catching a Q string because you can throw objects and catch objects. So we're throwing a string, catching a Q string, but it still says unknown error had occurred. Why is that? That's because we're not actually throwing a Q string. We're throwing a C-style string or a standard string. So let's actually go catch, really cat, quit biting my foot. Anybody want a free cat? I'm about ready to give one out. I'm just kidding. I love my cats. And believe me, these cats were very, very helpful when I was working on that shower. And we'll say each C error. And we're just going to debug this out into the console. So this time when we run it, maybe, if it builds, it'll say the cat ate my homework. Now, why is it suddenly saying that? Well, remember, we are throwing a standard string here. And here's where we're catching it. See that pointer 2? And that's where we're throwing it out. Now, let's modify this really quickly because I want to cover some other things. Say Q string. We're just going to throw this in here for illustrative purposes, just to show you the difference here in case some of you are out there scratching your head. And I know a lot of you are probably rolling your eyes going get to the point. As you can see, it's not executing that Q string portion right here. So we are actually in this right here, catch, care pointer to string. Now, what we could do is something like this where we're actually converting it to a Q string. I should say we're casting it into a Q string or more to the point building a new Q string in memory and making a copy of it, throwing it out. Now it says Q string, the cat ate my homework. So you can see we're actually making a Q string now. That's how you can throw objects. Now, remember I started this whole thing off with the C++ Exemption class. Well, we're going to very quickly make our own Qt style. So let's do add new, class, choose, and we'll call this myError. Give it a base class of Q object. Now, why would you want to do this rather than use the already built-in standard library? Well, the answer is simple, signals and slots. Sometimes you want to do things that the standard library just won't let you do. So what we're going to do here is we are going to just say include Q string. And if you remember, the standard library Exemption class had a string called what? We're going to be a little different here called string message. And then we're going to do a void set message. So now we have a class that we can store a string in. And of course, being a class, you can put just about anything in here. You can put other classes. So if you've ever worked with like Java or Visual Basic or C sharp or Python, you'll know that even an error is an object. Or you should say Exemption is an object. That's essentially what we're doing here. So we're just going to say Q string. And whoops. Now let's implement these real quick here. And let's go to our factor. Let's go to our new mouse pointer. Now we're just going to return it. So we'll say return. Very simple, very easy. Now, this goes into my little speech of doing it right the first time. Why would you do this? Why wouldn't you just take this variable and throw it into public? Well, the answer to that is simple. It's called encapsulation. You want to make sure that your users cannot directly modify your variable. The reason for that is simple. Let's go in here. Every time they set the message, you want to write to a log or, you know, append something to it or do something funky. Well, the problem with that is if they can directly modify that variable, they're going to totally wipe away your scheme. Also, anytime you make changes, you don't want to break this signature because they're going to be using this class. In other words, if you say set message and they've been using this for five years and then suddenly you come in and go blah, blah, blah, and add more variables, well, all their code suddenly breaks. That's why things become depreciated when you're using a library or framework because they want to move on to bigger and better things. In doing that, they don't want to break backwards compatibility. So let's actually add in our class here. We're back into the main.cpp. All right. Let's get rid of that real quick. Mm-hmm. Sorry, my cat is biting my foot and it's really, really, really getting on my nerves. Okay. There we go. I don't know what the deal is with this cat, but it ignores me until I sit down and then it's like, you know, WWF wrestling time at my house and I'm just like, go away. Leave me alone. All right. So we're going to say my error and I know some of you out there are going to be screaming, oh my gosh, I'm making a memory leak and you are absolutely right. I am. No real reason for it other than I'm just making a memory leak. Set message and we're just going to say lol. Now we're going to throw c error. I'm going to throw our object here. So what we're doing is we're making a new c error. We're setting the message and then we're throwing it. So we're actually throwing this object. Now in real world production code, if you don't want a memory leak, you would use the cue pointer object or you would put this inside of a class like you would have a main class that starts up and then you would use this, meaning you would make this a child of whatever class you're putting it into. Let's just give this a good build and run because we've added a new class. It's going to take slightly longer to build and it says, unknown errors has occurred. Well, we're throwing our c error. We gave it a message of lol. Why is it saying unknown error occurred? Well, that's simple. We haven't caught it. We haven't handled it. So just like we handled these other things, we have to do this. You notice how it says unknown error. This is our catch-all. So it goes through this entire stack and says, is it this? No. Is it that? No. Is it this? No. Then it's an unknown error has occurred. But we know what this is. So we're going to say, my error, we're going to just call this and you try to give your variables distinctive names just so you don't get them confused. We'll call this my error. Now when we give it a build and run, operator could not be found. Oh yes. Because we didn't do any operator overloading. Which kind of goes out of the scope. So what we're going to do here is just say message. If you're wondering, does not have an overloaded member. Let me pause this for a minute and figure out what I did wrong. Okay. Now I feel thoroughly, thoroughly stupid. If you look at this code, I'm going to explain what I did wrong here. And look at this code. What's the difference here? See if we can kind of scrunch all this on one screen. My error pointer to a new error object. Here we're saying my error copy of. So, like I said, do it right the first time. But this is why you would add error handling. It's going to make you really think about your code. And you're going to structure it in a manner that you have to think about it. Now when we build it, it should run just fine. There we go. My error equal LOL. Glad that's over with. So that in a nutshell is error handling. So real quick review. We just got a little application here. We got a test function. And let's jump into this. We've got in my error class. And it's just got a function that returns a string, a function that sets a string. And then we've got our protected variable my string. And then we're just showing basic error handling, which if you've done cute and nothing but cute, this is probably new to you because cute really doesn't do a whole lot of error handling. So I'm really glad he wrote in about that. That was a really good suggestion after I thought about it. Now one thing you'll notice is we're throwing our my error object or c error. And the cat ate my homework has never fired off. Let's run this again. I want you to really understand that. Starting my error equal LOL. But it never says the cat ate my homework. Why not? Well, because once you throw an error, it immediately jumps into the catch blocks. And it just goes, it's like a switch statement. It says, is it this? Yes, no. Is it this? Yes, no. Is it this? Yes, no. And likewise, if we reverse these, what do you think it's going to say? Tick tock, tick tock. It says the cat ate my homework because we're now throwing a cue string. The first one to get thrown is the one that moves the context of execution out of this block and into these blocks down here. Now, that's really it for this tutorial. Like I said, in a production one, you could use a cue pointer or, you know, the this mark. You could kind of modify it. But sometimes I'm kind of doing this just to see. Sometimes cute does not like doing this. Or if you don't use a pointer to an object on a cue object, sometimes it gets kind of cranky and says you can't copy the object. Yes. Initialize and connect convert from my error star. Yeah. My daughter would say that I've totally lost my mind going into management. I haven't thought straight since I started it. Here we go. Cannot access private member declared in cue object. And if you're wrestling around with this error message, you don't know what that means. Basically, you're trying to make a copy of this variable right here, which it cannot access it because it's, you know, internal. So let's just flip back here. I'm going to fix this back up. That was my washer if you hear little chiming music in the background. So it's time for this tutorial to end. I'm going to fix this code back up here. Make sure everything runs and then we'll post it back in. That's a cue string. That would be bad if we edited that one. Let's rerun this. There we go. Now life is good again. Now you know why I made that variable as a pointer rather than as a non-pointer value because you cannot copy an internal object without doing cloning, which is an entirely different ball of wax. C++ is a very complex language. Anyways, this is Brian. Thank you for watching. I hope you found this tutorial educational and entertaining. Once again, check out my website voidrealms.com. This source code plus the source code to I believe almost all my other tutorials is out there at no charge. I have to apologize. I'm going to be kind of offline for a while. I've really got to crank out the CCNA because well, you know, management came with a little bit of a pair raise, but a lot of responsibility and I have to get this I'm contractually obligated to get a CCNA within six months and the clock is ticking. So if you don't hear from me and if you're sending me emails or if you've sent me an email within the last three or four weeks and I haven't gotten back to you, well, I'm sorry. Work has to come first. Keep the emails pouring in. I will get to them eventually. Talk to you later. Bye.