Join Alexander Zanfir for an in-depth discussion in this video Introduction to exception handling, part of Learning C#.
- [Narrator] We now have enough experience working with C sharp and writing programs, that we know bugs and errors occur, no matter what. So let's take a look at how to handle them gracefully. There have been several reports of our application crashing from our clients. Mostly when a teacher is asked for a students grade, and they accidentally type in a letter. The application just crashes, and they have to start over. The user knows it's their fault but it would be nicer if the application handled it more gracefully instead of just crashing. Let's replicate that now so we know exactly what they are talking about.
So go ahead and open up the school tracker project, and then let's run it. (keys clacking) And in the grade we'll accidentally type in a letter. And you can see it's crashed. And we're getting a format exception saying "the input string was not in a correct format." So there's an unhandled exception. So let's handle it. It seems that if we try to parse or convert a string into an int when we didn't type in a number, the parse attempt will fail and throw an exception.
A quick solution to that is to use tryparse instead of just parse as we see on line 20. So let's try that. And tryparse is a little bit different in that it has two parameters. This first is what we're trying to parse into an int, and the second is an up parameter that will actually give us the result. Don't worry too much about how the up parameter works at this point, we'll cover that a bit later in this course. So instead of setting it with an equal sign, we'll just call tryparse, and set our variable by providing it as an up parameter and a second parameter.
And we also need to specify the out keyword. Now let's try that. So again, I will type in a letter instead of a number. And you can see it didn't crash this time. And if we go ahead and output the results, you can see the grade is set to zero because it was unable to parse it, it just used the default value of zero. So as we can see, now if it fails it no longer crashes with an unhandled exception.
Since it knows to just try it, and to just continue empty handed if it fails. The function also returned the true or false if it worked or failed. Let's check for a false and let the user know to try again. (keys clacking) So I'll set the results, which will be a boolean, which is be true or false, inside a variable called result. And then we'll just check result. And if that equals false, and a nice short form for that in C sharp is just an exclamation mark in front.
Which checks if result is false. And actually, if we wanted to check if it was just true we would just have result on its own. But in this case, we want to check if it's failed. And now we can just use a console dot right line to let the user know. (keys clacking) And let's try that. Now we will get a message letting the user know there's an error. But they won't have the opportunity to re-enter it just yet.
Also, keep in mind we need to switch to tryparse for the full number also. That is a good deal of extra code. Let's look at a way to handle exceptions for both the parse's we have in our program and to allow the user to input them again if it fails with a lot less code. Let's take a look at that next.
Explore variables and data types; controlling program flow with conditions and loops; and building functions. Learn how to implement object-oriented programming such as encapsulation and inheritance in C#, and find out how to debug your code. Alexander then explores advanced concepts such as enumerators, ref parameters, interfaces, events, and abstract classes. In the last two chapters, he covers the "top" features introduced in the last several versions of C#, including lambda expressions and string interpolation.
- C# variables and data types
- Switch statements
- Object-oriented programming: encapsulation, properties, and inheritance
- Debugging C# code
- Advanced C# concepts
- Top new features