Locked lesson.
About this lesson
We discuss how to deal with Errors within your code and how Exception Handling can prevent your program from crashing.
Exercise files
Download this lesson’s related exercise files.
39 - Error and Exception Handling.docx57.6 KB 39 - Error and Exception Handling SOLUTION.docx
58.9 KB
Quick reference
Error and Exception Handling
Exception Handling allows us to catch errors without them crashing our software.
When to use
Use these whenever you have a block of code that may cause an error.
Instructions
try
{
string[] names = { "John", "Mary", "Tim" };
Console.WriteLine(names[5]);
}
catch (Exception e)
{
Console.WriteLine("Error " + e);
}
Hints & tips
- Try / Catch block
- Exception handling is great for catching errors
- 00:04 Okay, in this video, I want to start to talk about errors and exception handling.
- 00:07 And up until now whenever there's been a problem with our code,
- 00:11 the whole thing just sort of crashes.
- 00:13 And sometimes you want that, right?
- 00:14 But most of the time you don't want your program to crash whenever it encounters
- 00:19 an error.
- 00:19 You want it just recognize there was an error and
- 00:22 then do something based on that error.
- 00:24 So imagine you've got software that's supposed to run for months and months and
- 00:28 months on end like bank software or something,
- 00:30 you don't want it crashing when there's an error.
- 00:32 So we can use something called exception handling to deal with our errors.
- 00:36 And that's what we're going to start to look at in the next few videos.
- 00:39 So basically, we're going to create blocks of code, and we're going to try them.
- 00:43 And if it doesn't work, we'll throw an error, right?
- 00:46 So the basic format is you start with try, and
- 00:51 then you've got your brackets, and Do something.
- 00:56 And then catch, and then Catch your errors, right?
- 01:02 So this is the basic format.
- 01:04 And really, you want to kind of start to put your code into blocks like this.
- 01:08 So then you can take care of catching your errors and stuff.
- 01:11 So let's play around with this.
- 01:14 So let's come through here and let's create, say, an array.
- 01:17 So string, and let's call this names.
- 01:20 And let's just set this equal right away to John, Mary, and Tim, right?
- 01:28 So we've got our array, there's three things in it.
- 01:30 0,1, 2. So
- 01:30 what if we tried to call something outside of this array?
- 01:35 We've seen this before, it throws an error, the whole program crashes, right?
- 01:38 So let's go Console.WriteLine,
- 01:42 and let's try and call names.
- 01:46 So this is 0, 1, 2.
- 01:48 Let's call the fifth one, right?
- 01:50 And down here,
- 01:51 this is where we write out whatever we want to do if there's an error.
- 01:56 So we could just Console.WriteLine and say, Whoops!
- 02:00 Looks Like There Was En error..., whatever.
- 02:05 So let's go and save this and run it, see what happens.
- 02:09 We see, Whoops!
- 02:09 Looks Like An Error.
- 02:11 Now, it looks like the program has ended, but it didn't end because of the error.
- 02:16 So I could show you that.
- 02:18 We can Console.WriteLine out something else.
- 02:21 We can go Press Enter To End.
- 02:22 And then, we can Console.ReadLine.
- 02:25 We've done before, right?
- 02:28 We just do something like this.
- 02:29 We save in right now.
- 02:32 We see Whoops!
- 02:33 Looks Like There Was An Error...
- 02:34 Press Enter To End.
- 02:35 See, the program hasn't crashed even though we've done something that should
- 02:39 cause it crash.
- 02:40 Because of our try and catch blocks.
- 02:41 So super important to start trying to put your code in these try blocks.
- 02:45 You don't always have to write your code in try and catch blocks,
- 02:48 but if you know there's an area where there might be an error,
- 02:51 you're going to want to start to use these things now.
- 02:54 We just caught this error.
- 02:56 We can also do something called Exception and then slap an e in here.
- 03:01 And this e variable, this will put the actual exception error code into this e
- 03:05 variable, and we can now use it anywhere we want.
- 03:08 So if we come up here, we can Console.WriteLine again.
- 03:16 And maybe we just want to pass on that e and see what it says.
- 03:18 Now, this will actually print out the exception.
- 03:20 The exception code, the exception error.
- 03:23 And so if we go ahead and semicolon, if we save this and
- 03:28 run it, We see System.IndexOutOfRange.
- 03:33 And that's what we tried to do here.
- 03:34 So this is our array, it has index numbers.
- 03:39 We tried to call the fifth index number, that's out of the range.
- 03:41 It only goes 0,1, 2.
- 03:42 It goes up to 2, we tried to call 5, that's an index out of range exception.
- 03:48 And then, we get this error code here, right?
- 03:51 That's this e right here and it also said, Whoops!
- 03:53 Looks Like There Was An Error...
- 03:56 Press Enter To End.
- 03:57 We do that and the program ends.
- 03:59 So those are exceptions and exception handling, very useful.
- 04:02 And you really just want to get into the habit of thinking about writing your code
- 04:07 with errors in mind, right?
- 04:09 And anytime you're in a place where, hey, this might throw an error,
- 04:13 you're going to want to try, catch block of code here.
- 04:17 And sometimes you use this exception e to get the exact exception.
- 04:22 Other times you won't need to.
- 04:24 We'll talk about when and how and
- 04:25 what that's all about probably starting in the next video.
- 04:28 But yeah, pretty cool.
- 04:29 So that's all for this video.
- 04:30 In the next video, we'll look at catching specific exceptions,
- 04:33 why that's important, and how we can do that.
- 04:36 That's coming up in the next video.
Lesson notes are only available for subscribers.