Locked lesson.
About this lesson
The Finally part of Exception Handling will ensure specific code runs after try / catch blocks.
Exercise files
Download this lesson’s related exercise files.
41 - Finally Exception Handling.docx61 KB 41 - Finally Exception Handling SOLUTION.docx
59.2 KB
Quick reference
Finally Exception Handling
Finally allows you to continue with your code after the try/catch blocks, regardless of the exception.
When to use
Use this when you aren't really sure what exception is going to be thrown.
Instructions
try
{
string[] names = {"John", "Mary", "Tim"};
Console.WriteLine(names[27]);
}
catch
{
Console.WriteLine("Error");
}
finally
{
Console.WriteLine("Continue on with your code...");
}
Hints & tips
- Finally allows your code to continue on after the exception.
- 00:04 Okay, so we've talked about exception handling, basic exception handling.
- 00:07 We've also talked about catching specific exceptions.
- 00:11 In this video I want to talk about finally.
- 00:13 And finally allows us to sort of continue on after we've caught
- 00:18 our exception if we're not exactly sure what we want to do, right?
- 00:22 So in the last video, we talked about catching specific exceptions.
- 00:26 There may be instances where you just aren't able to do that.
- 00:28 You can't guess all the different exceptions that are going to get called.
- 00:32 But you still want to continue on and do something afterwards.
- 00:35 And I don't know how many exceptions that are available.
- 00:38 We'll look at that in the next video, there's a lot.
- 00:40 So you're not going to memorize them all.
- 00:41 You're not going to catch them all every time.
- 00:44 You're not going to create code for every single exception, for
- 00:47 every single instance, because there's just too many of them, right?
- 00:50 So you pick the ones that you can kind of guess.
- 00:52 Like in the last video, we can kind of guess those two exceptions that we did.
- 00:56 The dividing by 0, and the entering a letter instead of a number.
- 01:01 That made sense in that specific scenario.
- 01:04 Like I said, though, there are incidences where you just may not know what's
- 01:08 going to happen, that's where finally comes in.
- 01:10 So let's do a quick try here, and also catch while we're at it.
- 01:16 And let's come back here.
- 01:18 And I don't know, let's go ahead and create another names array.
- 01:23 And same thing here, let's go,
- 01:27 John, let's go Tina, and let's go Jeff.
- 01:31 Mixing up the names, keeping it interesting.
- 01:34 I don't know.
- 01:35 So inside of here maybe we want to Console.WriteLine(names),
- 01:41 and we want the 27th name, right?
- 01:44 Obviously there isn't one, we're going to get an error.
- 01:47 So here we can Console.WriteLine().
- 01:52 And we could go, whoops, there was an error, whatever.
- 01:59 And if we save this and run it, we know exactly what's going to happen here.
- 02:04 We get, whoops, there was an error, and then it continues on, the program ends.
- 02:08 So you can do a lot of stuff in your catch block.
- 02:11 But again, there are some times when maybe you don't know what's going to happen.
- 02:15 Maybe you don't want just a little message here.
- 02:18 We want to continue on and keep going with some other things.
- 02:22 What can we do?
- 02:23 Well, we can go finally, and it's just another block of code.
- 02:26 So we can go Console.WriteLine().
- 02:29 And Finally moving on, dot dot dot.
- 02:34 And if we want to Console.ReadLine() like we've been doing, we can kind of do that.
- 02:40 Eh, maybe we also want a, Press Enter To End Program, whatever.
- 02:48 Save this, run it.
- 02:51 And we see, whoops, there was an error.
- 02:53 Finally moving on, press Enter to end program.
- 02:57 And if we kind of pull this over here, we can see the flow of this thing,
- 03:00 it starts at the top.
- 03:02 We've got our thing here, we've got an array.
- 03:04 We're trying to call an index out of bounds or whatever that was, right?
- 03:08 So it catches the error and says, whoops, there was an error, and
- 03:13 then we continue with our finally block.
- 03:16 And we, finally moving on, and we see, finally moving on,
- 03:20 press Enter to end the program.
- 03:22 And if we do that, if we hit Enter, the program ends, and that's that.
- 03:26 So that's finally.
- 03:27 Again, it's just a way to sort of execute some code after the try and catch.
- 03:32 If there's something you want to execute, you don't know the exact exception, and
- 03:37 you want to do just anything afterwards.
- 03:39 It's good for that, and pretty simple.
- 03:41 So that's all for this video.
- 03:42 In the next video, we'll look at a list of exceptions.
Lesson notes are only available for subscribers.