 Hey everybody, this is Brian and welcome to the 17th C-Sharp tutorial. Today we're going to be discussing error handling because, well, let's face it, bad things happen in programs. So to do this, let's just say int, and we'll say x equal 1, and int y equals 0. And we're going to create what's called a division by zero error, meaning you cannot divide something by zero, so it's going to crash our program. And we're going to say int, we'll say, actually let's say double z equal x divided by y. Now everything looks fine, there's no little red squiggly, no warnings, but when we go to run this, well, bad things happen, attempted to divide by zero. That means your program just crashed and died. And you see in the locals window over here, you have arguments, string zero, x1, y0, z0.0. And then it's telling you the call stack, something bad happened on line 15, et cetera. So all you can really do at this point is just close your program and figure out how to handle this. So now we have to figure out how to avoid this situation. So what you want to do is select your code, and you can right click and surround with, and just choose try, and that'll add the code for you. So it says try, code black, catch, exemption, code black, and you got to do something here. So let's give that exemption a name, we're just going to call it e, we could actually just call this error if you want to, make it a little clearer what it does. And let's just say console, right line, and let's say e, oops, error dot message. So let's discuss this a little bit. Let's figure out what's going on here. What we're saying is try. That is a try block, meaning this code's going to try to execute. If something bad happens while it's executing, it's going to catch that exemption. An exemption is an error. So we're going to say the exemption class, we're giving it a name of error. And then we need to do something when we catch that error. Console dot right line, error dot message, and error has a bunch of properties. As you can see, the exemption class is actually pretty big. You can get the inner exemption, the stack trace, I mean you name it, you can do it. But for this, let's just stick with message. We just want to know what happened. So let's run this again. Whoops, we forgot to console dot read. And of course it says attempted to divide by zero. So as you can see, you can try some code. You can catch the error and let's say 55. Now there was no error, so the program opens and closes instantly because it said try. It ran this code and it succeeded. Meaning the catch block was never called. You can add another block called finally. Whoops. And what finally does is finally always, always, always runs. Meaning doesn't matter if try runs or catch runs, finally we'll always run. So in this case we run it and we get just a, if I can grab it, a black window because we haven't printed anything out. So let's go ahead and go console, right line, and let's just print out the value of z. So what we're going to do here is we're just going to print out the value of z or we're going to print out the error message and we're always going to read. So the value is zero and then we know what these do. So what happens if you want to intentionally throw an error and you don't want to do some funky division by zero thing? For example, let's just say you want to do a hello world. After hello world, you want to say goodbye. But you intentionally want to raise an error. You want to know that something happened and you want an error condition to exist. What you use what's called the throw keyword. Kind of like when you're mad, you throw something against the wall. Well, don't do that, especially in my house. Throw new exemption and we're just going to give it a name here. We're just going to say goodbye cruel world. Now you notice how console down here is suddenly highlighted in green. It says unreachable code detected. That's because when you throw, it's instantly going to go down into this catch condition. So you can actually just get rid of that because you know as soon as it says throw, it's just going to jump down to catch. Now we can leave that in there and run this and see exactly what happens. It says hello world, goodbye misspelled of course cruel world. Let's fix that misspelling here. So this never runs. It never runs because we threw an exemption and the exemption automatically throws us down to catch. Now if we don't have catch in here, let's actually delete that and let's try running this. You guessed it, crashes our program again. Goodbye cruel world and it generates our error. So that's how error catching and error handling work in dot net. Pretty simple, pretty powerful. You can generate your own error messages. That's typically if you are creating your own class and you want to create an exemption condition, meaning like age would be a good example. Somebody enters 3000 for an age. Well obviously they're being a punk and you want to say hey, can't do that. So you throw a new exemption and saying your age must be between one and say 99 or whatever. That's all for this tutorial. Hope you found this educational and entertaining and thank you for watching.