Exception Messages3:02 with Jeremy McLain
Storing a message in the exception object is one way to add information to the exception.
Exceptions should be used to communicate about an error that has happened. 0:00 We can then use this information to instruct the user on 0:04 how to resolve the issue, or 0:08 we may be able to use the information to try to resolve it on our own. 0:09 Exceptions often happen as a result of programming mistakes. 0:14 In that case, exceptions are used to communicate to the programmer 0:18 that there's a bug that needs to be fixed. 0:22 In any case, 0:24 it's best to provide as much information about what may have caused the problem. 0:25 There are a number of ways we can add additional information to exceptions. 0:30 One way we can communicate what happened, 0:36 is by passing a message to the exceptions constructor. 0:38 Most exception types will allow you to pass a string 0:41 in as one of the constructors parameters. 0:44 We can do that. 0:46 Let's go back to the map location class and make this change. 0:48 We'll type, x, y is outside the boundaries of the map. 0:52 Now back in main where we catch this exception, we can access this message. 1:03 Up until now, 1:09 we haven't needed to have access to the actual exception object that was created. 1:10 In order to access this message, we'll need to get this object. 1:15 We do that by declaring a variable for the exception. 1:19 You can name this variable whatever you like, but 1:23 conventionally, many coders name it EX. 1:25 Now, that we have an exception object, 1:29 we can use it to access the message stored inside of it. 1:31 Let's print it to the console instead of this message. 1:35 Let's run the program again to see what this looks like. 1:41 There we go. 1:46 Now we've printed out the message that was inside the exception object. 1:47 In a typical program, there are lots of ways for errors to occur and 1:51 lots of things can throw exceptions. 1:55 We often want to handle errors differently, 1:57 based on the type of error that occurred. 2:00 Messages are nice for humans, but 2:02 computer programs rarely have the ability to read and understand English. 2:04 That's why we have multiple types of exceptions. 2:09 We've seen some of these exceptions in previous courses. 2:13 The system.exceptionclass is the most general purpose exception type. 2:16 Really all it means is that an error occurred. 2:21 It says nothing about the type of the error. 2:24 There are more specific types of exceptions that imply 2:26 that certain types of errors have occurred. 2:29 For example, 2:32 there's the system.format exception which is thrown from methods such as ent.parse. 2:33 Which is expecting a string to have a certain format so 2:38 that it can convert it to an integer. 2:41 System.exception and system.format exception 2:43 are both exceptions that are provided by the .net framework. 2:48 We can create our own exceptions though. 2:52 It's as easy as creating a new class. 2:54 Let's take a quick break and 2:57 see how to create our own exception types in the next video. 2:58
You need to sign up for Treehouse in order to download course files.Sign up