 This section is going to be about exceptions and let's take a look at this here. So we're going to say new console app Keep five design patterns. This is three one and I'm just going to paste this in here So exception safety and you can look that up on like Wikipedia here Basically, there's four types. There's no safety basic strong and a no throw guarantee And we're going to go over each one of these this video focuses on no safety This is where really bad things happen. This is um This is where only the brave dare to fly. Let's put it that way So this is where our program is probably going to crash We're going to create all sorts of issues and we really just don't care We're just hacking out some code trying to get it done as fast as possible. So I'm going to say get int and We're just going to make a couple little functions here and We're going to say Perfect code that never fails I'm going to try and give you an example of what we're talking about As far as something that we would expect would never ever fail on us So We can create a bool We're going to create an int called value equals and we're going to say Let's actually say q string data So I'm going to say data To int Now this is a little bit dangerous here because it could just You know massively fail. What if it's like hello world instead of an actual number? So we're going to say Give it a reference to that bool Now we need to see if it failed Otherwise, we're going to return the value Now I say perfect code that never fails But yes things bad things could happen because we're depending upon other people a.k.a the cute developers To not fail in this conversion function here, but the rest of this looks pretty pretty safe Now this is not necessarily a no throw guarantee, although it's very close to it We're going to Live in the real world and we're going to show An example of really what not to do, but you're going to see tons of it out there So I'm going to say boy I am and let's go ahead and I want to say q text stream And I should probably You know put some includes in here Probably be super helpful at this point And then let's go ahead and go include. Why not? Let's use a q list and Don't really need it, but just because I'm paranoid. All right. So let's go ahead and grab our q text stream And this is going to be a pointer to a stream Instantly working with a pointer We've kind of go. Oh no bad things are going to happen. Nobody loves pointers And we're going to work with a pointer to a list So instantly right off the bat. What's one thing that could go horribly wrong? That's right. There could be Null values or these pointers could be dangling pointers and they're not set which is very very dangerous So what we're going to do is just completely ignore that because why not? Yeah, let's actually let's make this a little more complex than I wanted to enter a number and a name and then just like An example here So now we're going to do some sort of data parsing. We're going to say q string Value equal Read line now. You notice how we're already using this q text stream, but we haven't even determined if it exists So this is super super dangerous So danger danger bad things could happen right there and then q string list items Equal value And we want to just split that Notice how we're using q string list But we don't have it in our includes. So that's another thing that could go horribly wrong. We're just assuming is included Now this will probably spark a whole controversy in the q&a where people are going to say well It is included because of the way qt does this this this and that and it's part of qt core and blah blah blah What if qt changes? I've run into that myself where I thought something was included I switched versions of qt and suddenly magically it's not included anymore Uh, that was a long time ago, but it did happen. So and we're going to say items at Let's go ahead and grab zero We're just going to say two int And we're just going to assume this works And i'm just putting my thoughts in the comments right after, you know, what each line what could possibly go wrong here Some of these things there's just way too much to name, but and we're going to do not append At there we go. We're assuming This exists it may not exist there may not be an item at one So What if they only entered one thing, you know, and they didn't hit space They just hit like the letter a so then we've got two problems. It's Not a number and there's no second value here Then we're just going to say list dot Insert And we want the id and the name We're assuming we even made it that far And then we're just going to come back and uh again List may not even exist because we're using a pointer. So another bad thing could happen there You info You entered and then We were probably going to have and i'm just going to Rather than type all that out just paste in for my notes here What's going on here? Well, what if we entered a number? So we've gotten all the way down to this point, but we're now seeing list at whatever that number What if we put in 257, but there's only one item in the list? Well, we're going to get an assert failure The index out of range error. So that will be spectacular And then we're going to just say user io and then stream list So we're just going to Call this function over and over again until we die So many horrible things in this little function that could just go completely wrong And it says all past this function will call itself. So now We're getting this systemic issue here. We're saying, uh-oh you are doing this incorrect, sir, and you may die So even the compiler at this point is going. Hey, buddy. Are you sure you want to do this? qtech stream And we're going to just read that this is a memory leak and potential crash Bad things are going to happen there. And let's go ahead and make our q list And let's call this a string. We could have said q string list Now we're just going to call this user io And we're going to say stream and list And of course Last thing on our notes here No matching function call. Uh, what are we doing wrong here? Oh That's why Reference to there we go So we have Something on the stack something on the heap. We're mixing and matching them which normally isn't an issue But we're just assuming they both exist Um, we're saying enter a number and a name. We're just going to read that entire line that could block forever and This may not even exist q string list item split not a big deal there But we're just assuming we got a value in the first place and then we are saying, okay Convert to an int. We're just assuming that even worked And then we're even assuming that there's even a second item here And then on and on I mean just a whole bunch of disasters that could just completely destroy us here But save run Does it work? Oh It did not You're saying, ah, I know what the problem is. It's that index out of range So let's say I came in and said zero brian Now it magically works one tammy magically works two dog magically works three cat What if I do 54? bob Dies so this is an intrinsic problem We have when writing code is that we just assume things will magically be there to work for us And then we are shocked to find out Something bad happened So when we talk about No safety that's really what we're talking about here is there is no Guarantees or warranties of any kind that this will not completely just obliterate our program