From the course: Learning C#

Exceptions - C# Tutorial

From the course: Learning C#

Exceptions

- [Illustrator] Just like in life, sometimes things go wrong in our programs and it's necessary to handle those conditions so that the user doesn't get a bad experience. In C#, like in some other languages, we use a programming technique called exception handling to catch errors before they make their way to the user. So let's take a look at how this works. Here in my code in the exceptions folder, I'll open up the Program File. So here in my example code. I have a few variables with some values and I'm going to do a simple math operation. I'm going to assign the result to the value of X divided by Y. So that works fine, just as long as I don't try to do something foolish, like divide by zero. So let's make that change and see what happens. So I make y zero and save this and let's go ahead and bring up our terminal, and what I'm going to do is run this now. So when I run the code, I think that's going to try to execute the program and Oh, no, that's not good. We get an error and you can see right here, there's a unhandled exception attempted to divide by zero. Well, we don't want to subject the user to that. So what we can do is we can handle this situation much better by using an exception handler. So let's close the terminal. What I'm going to do is wrap my code in what's called a try block, because I'm going to try to run this code. So I'll write, try and then I'll move this code inside the try block. And then what I'm going to do is, I'm going to define a catch block and that's going to catch any problems or exceptions that happen inside the try. So, if something goes wrong with any of the statements in the try block, then the execution is going to transfer down to this catch block here where I can handle the error before the user sees it. So in this case, I'm just going to print out a nice error message. So I'm going to print out, whoops. And, here we go. So let's go ahead and bring our terminal back up. And now try running this again. And now you can see that instead of this ugly looking exception we get this little message here that just says, whoops. So now we're printing out a nice error message. And in real life, you wouldn't do this. You would handle the error much more gracefully than this, but this is just a demonstration. So this is a generic catch handler, which is going to catch any exception that happens in the try block. But I can actually specify the type of exception that I want to catch cause there might be more than one. In fact, there's an exception just for this case. So I'm going to catch the divide by zero exception and I'm going to assign that to a variable named e, which I can then use inside my handler, so I can write out whoops, but I can also console right line e.message. So exceptions are objects. They have properties on those objects, just like any other object. And so I want to print out the message and I can also add other catch handlers for other exception types. So let's suppose I wanted to limit the value of X to 1000, and trigger an exception for larger values. So what I can do is I can write if x is greater than 1000, then inside the if statement, what I'm going to do is trigger the exception by using the throw statement, and I'm going to throw a new argument out of range exception, and that's going to take two parameters. The name of the variable or the parameter that's out of range, and then a message. I'm going to say, X has to be 1000 or less. And then I'll add my exception handler for that. So I'll write catch, and this is going to be the argument out of range exception. And again, I'll call that e and then I'll console that right line. Sorry 1000 is the limit. And then we'll also just go ahead and print out the messages like we do here. So I'll copy and paste that. So before we run this, there's one more part of the exception structure we can add, called a finally block. So I'll write finally and that's not going to take any arguments. So, this code is always going to run and I'll just simply print out a message to prove that. This code always runs So even if there's no exception if the try block works great, doesn't really matter. If any of these catches happen, this code in here is always going to run and it's a good place to clean up any resources like open files or anything else that needs to be disposed of. Okay. So let's go ahead and change X to a number that's larger than a thousand. So make 1,002 and let's go high back down to our terminal, run this again. And now you can see that my exception handler for the argument out of range, exception is triggering. It says, sorry, 1000 to the limit. And then it says X has to be 1000 or less. And then it tells me parameter X was the problem. And you can see here this code always runs that's from the finally block. So exceptions are used throughout C# and .Net and they provide a good mechanism for grouping error handling code together which keeps the main program logic, easier to read.

Contents