Locked lesson.
About this lesson
By anticipating specific kinds of Exceptions, you can program your code to treat them in unique ways without crashing.
Exercise files
Download this lesson’s related exercise files.
40 - Catching Specific Exceptions.docx61 KB 40 - Catching Specific Exceptions SOLUTION.docx
59.3 KB
Quick reference
Catching Specific Exceptions
If you know what exception your code will throw, you can take specific action.
When to use
Do this when you know what exception your code will throw.
Instructions
try
{
Console.WriteLine("Enter A Number");
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Another Number");
int y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(x / y);
}
catch (FormatException)
{
Console.WriteLine("Hey! Enter a Number not a Letter!");
}
catch (DivideByZeroException)
{
Console.WriteLine("Hey! You can't divide by zero!");
}
Hints & tips
- You can take specific action based on the exception that occurs.
- 00:04 In the last video, we started talking about exception handling.
- 00:06 In this video, I want to talk about catching specific exceptions and
- 00:10 why that's important.
- 00:11 So in the last video, we just sort of caught all the exceptions and
- 00:14 threw up something.
- 00:16 But there are lots of circumstances where you might want to take specific action
- 00:20 based on the type of error or exception that it throws.
- 00:23 If it throws this type of exception, do this, if it throws that type of exception,
- 00:28 do that.
- 00:28 So we're going to look at how to do that in this video and also how
- 00:31 to figure out hey, which exceptions might be thrown at any given time.
- 00:35 So let's come through here and let's create a try block.
- 00:38 And we might as well while we're at it, try and catch,
- 00:41 and here we're going to want the exception.
- 00:46 We'll just pass that as e.
- 00:48 And here let's just Console.WriteLine out that e.
- 00:53 So let's come up here and let's ask the user to enter a couple of numbers, and
- 00:57 then we'll do some math to those numbers and see what happens next.
- 01:00 So I'm just going to create an integer called x and
- 01:04 let's have that equal convert.to int 32.
- 01:08 We've looked at this before.
- 01:10 So then here we can just Console.ReadLine, right?
- 01:14 And so on top of this, let's go Console.WriteLine.
- 01:19 Let's say enter a number, right?
- 01:24 So it'll say enter a number on the console,
- 01:26 then this will allow us to type in a number and assign it to x.
- 01:30 And let's go ahead and just copy this whole thing and do it a second time here.
- 01:37 And let's say enter another number, and we'll call this one y,
- 01:41 and that looks good.
- 01:42 And then let's just Console.WriteLine x divided by y, okay?
- 01:49 So we just want to divide these two numbers.
- 01:51 So what could possibly go wrong, right?
- 01:53 Let's go and save this and run it and see.
- 01:57 So if we enter 10, enter another number 2, it outputs 5.
- 02:03 So okay, that looks good, no problems there.
- 02:06 Well, let's run this again and let's fiddle with this a little bit.
- 02:08 Let's try and act like a hacker might enter a number 10.
- 02:12 So let's enter another number g.
- 02:14 G, is not a number, so we get a format exception, all right?
- 02:19 Well, okay, that's interesting, so let's close this window.
- 02:23 Let's run this guy again.
- 02:26 And let's enter number 10.
- 02:28 Let's enter another number, 0.
- 02:30 Oops, we get another error, another exception because in math,
- 02:34 we can't divide by zero, we get infinity or some crazy thing.
- 02:37 But here we get a divide by zero exception.
- 02:40 So we're throwing two different types of exceptions.
- 02:42 Now we probably want to take different action based on those two exceptions.
- 02:46 With one we might want to say, hey you entered a letter,
- 02:48 you need to enter a number, right?
- 02:50 The other one we might say hey you can't divide by zero.
- 02:52 So based on the exception that gets thrown,
- 02:55 that's how we want to do something different.
- 02:59 So by having this in our code right here,
- 03:01 this exception e, we can figure out what errors are being thrown.
- 03:06 So we had a, let's see, we had a divide by zero exception.
- 03:11 We also had a, let's run this guy again.
- 03:15 Let's just type in g and we had a format exception.
- 03:20 So I'm just highlighting this, dragging my mouse while holding the button and
- 03:25 then hitting Ctrl + C on my keyboard, that's a shortcut for copy.
- 03:29 And I'm going to come down here and Ctrl + V, it's a shortcut for
- 03:33 paste or you can right click, and hit Paste like that.
- 03:35 So we've got two different types of errors.
- 03:38 How do we do different things based on each of them?
- 03:41 So let's start with this one.
- 03:42 Now I'm going to come down here and
- 03:44 I'm going to get rid of this little thing here because we don't need it anymore.
- 03:46 Now we know that the exception is, it's a divide by zero exception.
- 03:50 So we can just put that in there just like that.
- 03:52 And here, let's just console right out,
- 03:57 Hey, You Can't Divide By Zero, all right?
- 04:02 Now we can come down here and do another catch, all right?
- 04:08 And we can do the other one.
- 04:09 So the other one was a format exception, so we just paste that in there like that.
- 04:13 And same thing here, I'm just going to copy this whole thing.
- 04:16 Also write out.
- 04:18 Hey, You Must Enter A Number Not A Letter, right?
- 04:27 Very angry exclamation points, right?
- 04:30 So let's go and save this and run, see if that works.
- 04:34 So enter a number, let's type in H.
- 04:36 Hey, you must enter number not a letter.
- 04:39 Now the program ends because we haven't programmed it to do anything else.
- 04:42 But ideally, you would do something else in this block.
- 04:46 Call another function that you want to run.
- 04:48 Or have this whole thing be sitting in a function that you called again to run it
- 04:52 again or whatever you want it to do, you could do it right here.
- 04:55 So if we run this again, this time enter number 10,
- 04:58 enter another number zero, hey, you can't divide by zero.
- 05:02 So we can see very clearly that it's taking different action based on
- 05:07 what the specific exception is.
- 05:10 So very cool, very useful.
- 05:11 And this just gives you more power over exactly what happens in your code.
- 05:16 And again, we really don't want our program to just crash and end.
- 05:20 So this way we can really dive in here and figure out exactly what's going on,
- 05:25 take specific action because these things can happen.
- 05:28 You can have perfect code, but somebody can still type a g, and
- 05:32 when you ask them for a number and it flexes the whole thing, right?
- 05:35 There's nothing you can do about that,
- 05:37 people are going to do what people are going to do.
- 05:39 But at least this way you can sort of take steps to prevent it from causing
- 05:44 harm to your program and continue on without crashing the whole thing.
- 05:49 And that's why this is so important.
- 05:50 So in the next video, we'll look at finally exception handling.
Lesson notes are only available for subscribers.