1 00:00:01,550 --> 00:00:06,410 This video is surrounding exception handling in C sharp. 2 00:00:06,410 --> 00:00:11,060 I have already created the projects and we're calling this one exception handling so you can pause right 3 00:00:11,060 --> 00:00:11,350 here. 4 00:00:11,360 --> 00:00:14,510 Create the project and resume once you have done that. 5 00:00:14,540 --> 00:00:14,870 No. 6 00:00:14,880 --> 00:00:21,710 As an exception is a problem that arises during the execution of a program and it is not unique to C 7 00:00:21,710 --> 00:00:25,320 sharp which is pretty much in every single programming language. 8 00:00:25,340 --> 00:00:32,930 It's usually a response to you know invalid inputs are undesirable conditions for an operation so you 9 00:00:32,930 --> 00:00:40,730 may have encountered maybe the ABM where they asked you to insert your selection and you press one when 10 00:00:40,730 --> 00:00:47,510 you know you compress one but you press one and then if you see a big nasty arrow screen then that means 11 00:00:47,510 --> 00:00:52,850 there exception handling was probably poor because they didn't factor in that this could potentially 12 00:00:52,850 --> 00:00:53,540 happen. 13 00:00:53,570 --> 00:00:59,900 Now when you're writing your application you are the one who is determining the inputs and the you know 14 00:00:59,900 --> 00:01:03,790 the outputs and want more importantly you know the desired inputs. 15 00:01:03,800 --> 00:01:08,180 So there are situations that you can foresee a potential error. 16 00:01:08,180 --> 00:01:10,550 That's what exception handling is all about. 17 00:01:10,550 --> 00:01:15,380 You're going to write your code in a what we call defensive manner so that you see that under these 18 00:01:15,380 --> 00:01:21,290 circumstances going through a nasty error don't exit the program but should the user a nice error message 19 00:01:21,290 --> 00:01:27,560 to say hey you know you did something wrong when we could have been a potentially fatal operation for 20 00:01:27,560 --> 00:01:28,910 your program. 21 00:01:28,910 --> 00:01:36,020 So I've added some notes to this code file a the try catch finally and throw keywords all of which are 22 00:01:36,320 --> 00:01:40,230 keywords used when we're talking about exception handling mode to try. 23 00:01:40,250 --> 00:01:43,970 Basically is just a block that says I'm going to try this operation. 24 00:01:43,970 --> 00:01:45,470 I'm going to try and do this. 25 00:01:45,530 --> 00:01:51,400 The catch basically says catch in the arrow like stop and check if there's any arrow. 26 00:01:51,470 --> 00:01:52,430 After you try it. 27 00:01:52,430 --> 00:01:55,460 If there is an arrow then I want to take some action. 28 00:01:55,490 --> 00:01:57,830 The finally basically says that okay. 29 00:01:58,010 --> 00:02:03,770 After we tried it and maybe something was got maybe something was not caught I'm still going to do this 30 00:02:03,800 --> 00:02:05,400 operation anyway. 31 00:02:05,510 --> 00:02:10,220 And then the thrill is a key word that we use to end the execution. 32 00:02:10,220 --> 00:02:17,300 So we sometimes in programming you would try something and then you catch a particular kind of exception 33 00:02:17,570 --> 00:02:23,080 and then you the programmer know that if this exception is ever caught that is going to be detrimental 34 00:02:23,120 --> 00:02:24,950 to whatever operation is able to happen. 35 00:02:25,040 --> 00:02:32,720 So you can manually throw all the the program out as in when you do throw it will literally throw the 36 00:02:32,720 --> 00:02:35,690 arrow to the screen and exit the program. 37 00:02:35,690 --> 00:02:35,900 All right. 38 00:02:35,900 --> 00:02:38,960 So let's start this adventure with a small program I wrote. 39 00:02:38,990 --> 00:02:42,770 It's already taken the spores underwrites it off yourself. 40 00:02:42,770 --> 00:02:45,290 So we're just accepting two numbers. 41 00:02:45,290 --> 00:02:46,740 We're doing a console right line. 42 00:02:46,760 --> 00:02:51,640 Enter your first number and then I'm passing my integer in a different method. 43 00:02:51,650 --> 00:02:56,260 So we would have done convert dot to int 32. 44 00:02:56,270 --> 00:03:01,980 No I'm doing it another way where there is actually a focus on instead of the int data type called Dot 45 00:03:02,090 --> 00:03:08,330 pass and then it works pretty much the same way except the converts to in 32 has a wider range than 46 00:03:08,330 --> 00:03:09,030 a dot pass. 47 00:03:09,050 --> 00:03:13,490 But for the purpose of this example we can just use the dot pass. 48 00:03:13,490 --> 00:03:19,310 So we're asking the user for two numbers and then we're going to do some division and then we're going 49 00:03:19,310 --> 00:03:20,990 to display the results. 50 00:03:20,990 --> 00:03:30,020 No I'm going to run this program the first time and then we're going to see that 10 divided by two is 51 00:03:30,020 --> 00:03:32,570 going to be five and that's perfect. 52 00:03:32,570 --> 00:03:34,930 And we know that okay our application works. 53 00:03:34,980 --> 00:03:37,340 Now you're probably saying okay that's straight forward that's thunder. 54 00:03:37,370 --> 00:03:38,810 That's what was expected. 55 00:03:38,810 --> 00:03:41,060 What is the moral of the story. 56 00:03:41,060 --> 00:03:47,240 So let's execute this again and then this time I'm going to do something that I know is going to throw 57 00:03:47,240 --> 00:03:55,740 an exception and so I'm going to see 10 divided by zero and anybody who knows math knows that there 58 00:03:55,740 --> 00:03:59,390 is no division by zero no look at what visual studio does. 59 00:03:59,390 --> 00:04:06,240 When I press enter it throws what we call an exception AC exception on the handle. 60 00:04:06,270 --> 00:04:12,030 They're telling us that this video this lesson is called exception handling and they're telling us that 61 00:04:12,030 --> 00:04:14,930 this exception was on a handled system. 62 00:04:15,000 --> 00:04:19,910 Divide by zero exception attempt to divide by zeros at its end what the exception was. 63 00:04:19,920 --> 00:04:26,580 So once again there are certain situations under which your program is just going to freak out because 64 00:04:26,580 --> 00:04:28,970 those operations are illegal. 65 00:04:28,970 --> 00:04:29,460 All right. 66 00:04:29,670 --> 00:04:34,860 So even in programming even though we're telling the computer to do it the computer cannot divide by 67 00:04:34,860 --> 00:04:35,510 zero. 68 00:04:35,550 --> 00:04:42,180 So exception handling would allow us and so in it's in this state we have to just continue in Visual 69 00:04:42,180 --> 00:04:45,950 Studio and then it will get back to the states where we can start writing code again. 70 00:04:45,960 --> 00:04:51,780 I know we'll have to write a block that says let me handle the exception. 71 00:04:51,780 --> 00:05:00,030 So to start off we try so I can just say a try and then open with fairly braces and then I'm going to 72 00:05:00,030 --> 00:05:00,980 try and operation. 73 00:05:00,990 --> 00:05:06,180 So I kind of soft try and empty because you know this is our visual studio is telling me that it's incomplete. 74 00:05:06,180 --> 00:05:07,610 So what am I going to try. 75 00:05:07,980 --> 00:05:13,020 I don't need to try that input because I know that they're going to put input inputs but I'm going to 76 00:05:13,020 --> 00:05:16,720 try the operation which is the division right. 77 00:05:16,740 --> 00:05:20,640 So I'm going to try to divide. 78 00:05:20,850 --> 00:05:25,080 And then I am going to follow that up with a catch. 79 00:05:25,080 --> 00:05:30,840 No there are many exceptions and we just saw a very specific one we saw one that says system divide 80 00:05:30,870 --> 00:05:37,500 by zero exception you also have system that Io accepts and also a system that every time mismatch exception 81 00:05:37,740 --> 00:05:41,400 you have a vote of memory except when you have a number of exceptions. 82 00:05:41,430 --> 00:05:44,500 No I'm not telling you to remember all of them. 83 00:05:44,580 --> 00:05:46,800 I'm just saying that there are a lot. 84 00:05:46,920 --> 00:05:52,210 So they all come from one based class called exception. 85 00:05:52,240 --> 00:05:52,750 All right. 86 00:05:52,830 --> 00:05:59,080 So we just type exception we can literally just cover every base and then we're just going to say Exception 87 00:05:59,420 --> 00:05:59,930 e. 88 00:06:00,030 --> 00:06:01,940 So I'll explain what that is. 89 00:06:02,010 --> 00:06:08,460 But when we'd say the keyword exception is like a base class that covers every type of exception so 90 00:06:08,480 --> 00:06:15,120 much of the type of exception that is caught it will be caught by this class. 91 00:06:15,120 --> 00:06:19,530 No you are probably wondering okay so why would I need to specify the type of exception. 92 00:06:19,530 --> 00:06:25,440 Well there are times when based on the type of exception that is caught you want to do something different. 93 00:06:25,440 --> 00:06:33,480 So like the divide by zero you probably want to from them to enter a better denominator or not denominator 94 00:06:33,480 --> 00:06:34,600 advisor sorry. 95 00:06:34,770 --> 00:06:41,640 And if it is maybe a system not I owe exception then you want to bring something else so you can actually 96 00:06:41,640 --> 00:06:46,860 have multiple catch blocks so I'm just going to close the catch block so that's what the try catch looks 97 00:06:46,860 --> 00:06:47,100 like. 98 00:06:47,100 --> 00:06:52,120 So when people talk about taxing on handling or you hear somebody say try catch this is what they're 99 00:06:52,140 --> 00:06:52,780 talking about. 100 00:06:52,780 --> 00:06:58,200 So you try on Operation you catch the arrow and then you can actually catch many arrows. 101 00:06:58,210 --> 00:07:05,730 I can have multiple catches and each catch would just have a specific exception so I can specify that 102 00:07:05,730 --> 00:07:12,630 I want the division by zero exception to be caught in this catch block and then I can go on to see well 103 00:07:12,660 --> 00:07:14,670 actually the ordering here matters. 104 00:07:14,680 --> 00:07:15,720 So I'm sorry about that. 105 00:07:15,720 --> 00:07:21,920 If I want to be specific then I have to do the specific ones before I catch exception. 106 00:07:21,930 --> 00:07:27,960 Because then if I did catch exception before the divide by zero then catch exception would have been 107 00:07:27,960 --> 00:07:32,610 catching any type of exception anyway and it would never have gotten to the divide by zero. 108 00:07:32,620 --> 00:07:33,260 Right. 109 00:07:33,300 --> 00:07:39,630 So that then the order much as well like I will see if we just have exception like visual studio just 110 00:07:39,630 --> 00:07:44,400 indicated then we are catching all of the exceptions possible. 111 00:07:44,400 --> 00:07:50,730 No I did salesman to explain the purpose of the EA so I had EA and then I said EA x or whatever so this 112 00:07:50,730 --> 00:07:53,960 is pretty much just an object that represents the exception. 113 00:07:53,960 --> 00:07:59,520 So you notice when the exception message came up earlier they had some details to a name it had the 114 00:07:59,520 --> 00:08:00,300 content. 115 00:08:00,330 --> 00:08:02,380 Told us what the arrow was. 116 00:08:02,400 --> 00:08:10,290 So all of those components all of those bits of information we can all collect them and store it inside 117 00:08:10,290 --> 00:08:17,610 of some variable or some object called E and then that will allow us to will print or whatever we want. 118 00:08:17,610 --> 00:08:21,630 So let's say we tried this Operation All right. 119 00:08:21,630 --> 00:08:25,920 And then after trying the operation we called the exception. 120 00:08:25,920 --> 00:08:29,080 That said division by zero. 121 00:08:29,080 --> 00:08:36,380 So when I catch that exception I can choose to console right line and print so it's an error message 122 00:08:36,650 --> 00:08:38,840 illegal operation. 123 00:08:38,840 --> 00:08:44,990 So instead of giving that nasty arrow where it stopped the operation and started telling us about its 124 00:08:44,990 --> 00:08:52,400 system divide by zero decided that because that means really nothing to a user you can give a more elegant 125 00:08:52,430 --> 00:08:56,680 error message to say Hey man you know illegal operation right. 126 00:08:56,750 --> 00:09:06,200 So I'm going to try the operation then print the results and then I'm going to catch any arrow and then 127 00:09:06,290 --> 00:09:11,430 I can see if finally all right console the right line. 128 00:09:11,430 --> 00:09:19,290 So I just do a quick console right line and see I just copied and pasted that end of program so remember 129 00:09:19,290 --> 00:09:25,600 that the finally will execute regardless of the trying to catch operation so it's going to try this. 130 00:09:25,770 --> 00:09:32,190 If it's successful then it's going to do this finally if it tries it and there is an error that it catches 131 00:09:32,400 --> 00:09:34,340 then it's going to still do this finally. 132 00:09:34,370 --> 00:09:38,900 So let's try that again so we're going to do a good one. 133 00:09:38,940 --> 00:09:40,930 Ten and two. 134 00:09:41,520 --> 00:09:45,330 And then you see results and then end of program. 135 00:09:45,330 --> 00:09:48,060 So it tried it and then it did finally. 136 00:09:48,150 --> 00:09:55,230 Let's try that again so this time I'm doing 10 and 0. 137 00:09:55,520 --> 00:09:58,000 And then you see illegal operation. 138 00:09:58,010 --> 00:10:01,160 So give me my error message and then it still did. 139 00:10:01,280 --> 00:10:05,870 Finally some way to kind of me the error message a bit more explicit. 140 00:10:05,880 --> 00:10:06,170 No. 141 00:10:06,180 --> 00:10:10,160 Someone to see illegal operation and put a call on. 142 00:10:10,160 --> 00:10:15,680 And let me just use interpolation because I am going to be printing the actual error message from the 143 00:10:15,680 --> 00:10:16,300 exception. 144 00:10:16,310 --> 00:10:17,990 So exception is generic. 145 00:10:17,990 --> 00:10:23,260 Once again I don't know if it's a divide by zero or a system that I or sometimes you get system that 146 00:10:23,270 --> 00:10:27,890 Io exceptions they have 20 different arrows instead of that exception type. 147 00:10:27,890 --> 00:10:34,790 All right so sometimes it is good to know the exact error that is being thrown so that you can be better 148 00:10:34,790 --> 00:10:38,140 at debugging the application but sometimes it's not deliberate. 149 00:10:38,150 --> 00:10:39,030 I'm being deliberate. 150 00:10:39,030 --> 00:10:45,860 No but a lot of the times we write code and we're not being deliberate with the arrows and we're probably 151 00:10:45,860 --> 00:10:46,850 missing something. 152 00:10:46,880 --> 00:10:52,780 So I'm going to just say e dots and then I can see that message. 153 00:10:53,300 --> 00:10:58,810 So e the message is the actual message attached to the exception so the expectation is that when the 154 00:10:58,820 --> 00:11:05,560 exception is thrown or courts or other we're going to print illegal operation on call on and then the 155 00:11:05,560 --> 00:11:06,640 actual error message. 156 00:11:06,640 --> 00:11:14,600 So let's try that again so 10 and zero once more and then we're seeing here illegal operation cool on 157 00:11:14,810 --> 00:11:20,090 and then they're giving us the message attached to the exception which is that we attempted to divide 158 00:11:20,330 --> 00:11:21,260 by zero. 159 00:11:21,290 --> 00:11:24,320 So that's a nice way to handle arrows. 160 00:11:24,320 --> 00:11:27,380 And then once again the final will still execute. 161 00:11:27,520 --> 00:11:29,390 And as it will do what it needs to do. 162 00:11:29,390 --> 00:11:35,400 No we did say there's a keyword called throw and the throw and I'm went out to put this through above 163 00:11:35,420 --> 00:11:41,450 this console the right line notice so it kind of becomes though and if I hover over it then it's going 164 00:11:41,450 --> 00:11:48,080 to be telling me that it is unreachable seats is unreachable code detected because the fact is that 165 00:11:48,080 --> 00:11:51,410 the throw is just going to kill the program right there. 166 00:11:51,410 --> 00:11:51,760 All right. 167 00:11:51,890 --> 00:11:58,510 So when I say try and then catch exception if I say throw then it just kills the operation. 168 00:11:58,520 --> 00:12:00,980 There is no elegant error message. 169 00:12:01,370 --> 00:12:03,560 Let's even test to see if there is even a final. 170 00:12:03,590 --> 00:12:05,560 So I believe there is a finally. 171 00:12:05,560 --> 00:12:13,400 But let's just validate that so 10 and zero once more and then you see it just go straight to the. 172 00:12:13,410 --> 00:12:20,260 So it just stops the program comes to through and I mean visits to do is showing us what the problem 173 00:12:20,290 --> 00:12:26,320 is but then if your console application is not executing inside of goes through then there is there 174 00:12:26,380 --> 00:12:28,600 is none off the screen for the user. 175 00:12:28,600 --> 00:12:35,530 All right so it would just exit the program and they would never know why it exited the program. 176 00:12:35,550 --> 00:12:35,990 All right. 177 00:12:35,990 --> 00:12:41,910 And there's the on hand of the exception something and this is the error message that would come up 178 00:12:41,910 --> 00:12:44,430 for that user if we use through. 179 00:12:44,430 --> 00:12:50,730 So once again exception handling is a bowl watching for particular types of errors and handling them 180 00:12:50,910 --> 00:12:51,540 elegantly.