Catching Exceptions5:02 with Jeremy McLain
Learn the ins and outs of catching exceptions and what all this has to do with inheritance.
Now that we have multiple types of exceptions 0:00 let's look at how to handle them using try catch. 0:03 We already have a try catch block in the main method 0:07 that's catching system.exception. 0:10 As this code is written right now this catch block will handle any and 0:12 all exceptions that are thrown from the code inside the try block. 0:17 If a TreehouseDefense exception gets thrown, this block will handle it. 0:21 Even the out of bounds exception will get caught and handled here. 0:26 In fact, if any other exception type is thrown, 0:30 it will still be caught by this catch block. 0:33 We're not yet handling different types of exceptions differently. 0:36 This catch clause is meant to catch system.exception. 0:40 So you might be wondering why it will catch all of those other exceptions. 0:44 It's because all exception types are subclasses of the base system.exception. 0:49 So they're all of type system.exception and this catch clause will catch them all. 0:54 Typically we don't want to handle all exceptions the same way. 1:00 In order to handle different exceptions differently 1:04 we just need to add more catch clauses. 1:07 Catch clauses for the more specific exception types 1:10 need to be placed before the more general exception types. 1:13 We'll create a catch clause for our OutOfBoundsException here. 1:17 Let's also add one here for TreehouseDefenseException. 1:28 So now we can catch all three types of exceptions. 1:41 The order is important, you can't list more general exceptions 1:45 before more specific exceptions or else you'll get a compiler error. 1:49 This is much better than just catching system.exception because now we can print 1:53 something different to the console for each of these exception cases. 1:58 For the out of bounds exception, let's print what we did here. 2:02 You might be wondering why we're even catching these other two exceptions here. 2:08 After all, the only exception we're throwing is the OutOfBoundsException. 2:13 We have these other two catch clauses here to future proof our code. 2:18 Meaning, it's possible that at some point in the future, someone might 2:22 change the code to throw something other than the OutOfBoundsException. 2:27 By adding these catch clauses here to catch all possible exceptions, 2:31 we can display our own message to the screen instead of the big scary 2:35 one that gets displayed by default. 2:39 For now, let's just print Unhandled TreeouseDefenseException here. 2:41 And Unhandled Exception here. 2:57 We should remove the ex variable from these clauses since we aren't using it. 3:07 Otherwise, we'll get a variable not used warning when we go to compile. 3:12 Typically we don't want to catch such general exception types. 3:16 Usually, the only place that catching very general exception types makes sense, 3:21 is in the main method. 3:25 Because main is the last method in the program that can handle them. 3:27 This is used as a last resort to do something before the program crashes. 3:30 There are lots of tips and tricks, and 3:35 many best practices to learn, on how to do good exception handling. 3:37 Check out the teachers notes for more resources if you're interested. 3:41 It can sometimes be difficult to decide when to use exceptions and 3:46 how to handle them. 3:50 This was just an introduction, there's a bit more to C# support for 3:51 exceptions than what we've discussed so far. 3:55 We'll talk a lot more about exceptions and 3:58 other error handling mechanisms in other courses though. 4:00 The purpose of this course is to show how C# supports object oriented programming. 4:04 And exceptions are an excellent example of how inheritance and 4:09 subclassing are used in OO. 4:13 By using this principle of object oriented programming 4:16 we've created two different class hierarchies. 4:19 One with point and map location, and another with system.exception, 4:22 TreehouseDefenseException, and OutOfBoundsException. 4:26 We've seen how subclassing and inheritance can be used to reuse and 4:31 extend existing classes, without altering the original class. 4:35 We've also seen many examples of the is a relationship between classes. 4:40 A map location is a point, 4:45 the OutOfBoundsException is a TreehouseDefenseException, and so on. 4:47 Next we'll learn about another core principle of object oriented programming 4:53 called encapsulation. 4:57 We'll need it to finish building the Treehouse Defense game. 4:59
You need to sign up for Treehouse in order to download course files.Sign up