Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: C# error handling

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's look at a program
0:01 that's not very reliable
0:02 so it's going to need some error handling.
0:04 Here we have our run method for the Errors class
0:08 and what it's going to do is
0:10 it's going to generate a bunch of numbers
0:12 1 to 20 and it's going to add, I don't know
0:15 some Fibonacci numbers or something like that
0:17 on the end. And then it's going to try
0:19 for each one of those numbers
0:20 to call SketchyMethod.
0:23 The name might give you a hint
0:24 of what's happening here.
0:26 So it's going to set the color to gray
0:28 so you can see what's going on really well
0:30 and then it's going to set it to yellow
0:32 and if there's an error based on the error
0:34 it's going to say cyan
0:35 and then some kind of message.
0:38 So here's error handling in C#
0:40 we'll call a function and a try block.
0:42 And then we can distinguish
0:43 between the different types of errors
0:45 by having different catch blocks.
0:47 So there's a SocketException.
0:49 We have catch(SocketException).
0:50 There's a ValidationException
0:52 like some value is not valid
0:54 we'll get it down here.
0:55 If it's some unknown Exception
0:57 we can just catch it in general
0:59 and to find a variable
1:00 which will not appear
1:02 and we're going to print out
1:04 oh, there's some kind of error
1:05 we're going to get the type
1:06 say it's this type of error and here's the message
1:08 We were not really planning on this
1:09 but something bad happened
1:11 Pretty cool, let's run it and see how that works
1:15 There you go, you can see
1:16 we didn't have a lot of faith in it
1:18 so we run it, and it says
1:20 calling it with 1, hey that worked, 2 that worked
1:22 we call it a 6, for some reason, we got
1:24 an overflow or underflow arithmetic exception, right?
1:28 This was one that got caught down here
1:30 we didn't expect it, so we just
1:33 printed out the type in a message
1:35 Down here, we've got a network error
1:38 that was a socket exception that was thrown
1:40 We said there's something wrong with your connection
1:42 It worked for a while, network, and then arithmetic
1:44 and then lets see
1:46 In this one, we pass it 0 and it said
1:49 no, no, no, I can't work with 0
1:51 that's not something I can compute with
1:53 Okay, so here's error handling in C#
1:55 couple things to note, try, do stuff, catch
2:00 But the catch has to be most specific down to general
2:04 Because the way it works is the error's thrown
2:06 and C# says, does it derive from SocketException?
2:09 If yes, we go here. Otherwise, does it derive
2:12 from ValidationException. Otherwise, does it derive from Exception
2:15 Finally, yes everything derives from Exception
2:17 that can be thrown, so down here
2:19 we are handling this
2:20 So, you've got to make sure that you
2:21 have it more specific to more general
2:24 the way it goes down here, all right
2:26 This is our super reliable way
2:28 of working with this SketchyMethod