1 00:00:00,180 --> 00:00:01,020 ‫Welcome back. 2 00:00:01,050 --> 00:00:08,910 ‫In this video, you will learn how to catch errors and to do something when an error occurs and not 3 00:00:08,910 --> 00:00:11,670 ‫make your program crash or anything like that. 4 00:00:11,790 --> 00:00:19,710 ‫So a common usage of catch and finally together is to obtain and use resources in a tri block, deal 5 00:00:19,710 --> 00:00:24,680 ‫with exceptional circumstances and a catch block and release the resources in the final block. 6 00:00:24,690 --> 00:00:28,070 ‫And what that means is what we're going to cover in this video. 7 00:00:28,080 --> 00:00:33,870 ‫So in order to run into an error, let's do the following. 8 00:00:33,900 --> 00:00:36,720 ‫We use the console right line. 9 00:00:36,720 --> 00:00:44,790 ‫So enter CW double tap and here I say please enter a number and we're just prompting our user to enter 10 00:00:44,790 --> 00:00:51,630 ‫a number and then we want to store that in user input, which is just going to be a string variable 11 00:00:51,630 --> 00:00:59,820 ‫and it's going to read whatever the user has to enter or has to say, and it should be a number in the 12 00:00:59,820 --> 00:01:00,240 ‫end, right? 13 00:01:00,240 --> 00:01:01,680 ‫That's the goal that we have. 14 00:01:01,680 --> 00:01:11,670 ‫But if we do the following and user input as int and we use int, dot pass. 15 00:01:12,150 --> 00:01:19,860 ‫So you have seen pass already and we try to parse the user input that was given here by the user. 16 00:01:20,910 --> 00:01:29,280 ‫We try to make an integer out of it and now if you run that, please enter a number and during three 17 00:01:29,610 --> 00:01:37,320 ‫and our program is fine actually let's add a console dot read key here. 18 00:01:37,320 --> 00:01:40,350 ‫So we see even better that the program is still fine. 19 00:01:40,710 --> 00:01:42,480 ‫So please enter a number. 20 00:01:42,480 --> 00:01:46,620 ‫I'm just going to enter five and our program is still running and it didn't crash. 21 00:01:46,620 --> 00:01:49,230 ‫Everything is fine, so now let's run it again. 22 00:01:49,500 --> 00:01:54,570 ‫But instead of entering a number, let's add a letter or write some text. 23 00:01:54,570 --> 00:01:55,440 ‫That is not. 24 00:01:56,200 --> 00:02:01,000 ‫Passable into an integer what we're doing here in line 16 in my case. 25 00:02:01,000 --> 00:02:09,330 ‫So I'm going to enter and my program crashes and it says exception on handled system format exception 26 00:02:09,340 --> 00:02:12,100 ‫input string was not in a correct format. 27 00:02:12,400 --> 00:02:12,820 ‫All right. 28 00:02:12,820 --> 00:02:19,090 ‫So the string that was given by the user was not in the right format to parse it into an integer. 29 00:02:19,180 --> 00:02:22,300 ‫And that's what a format exception is all about. 30 00:02:22,750 --> 00:02:27,670 ‫Now we can handle that by creating a try and catch block. 31 00:02:27,790 --> 00:02:34,150 ‫So let's say we use try and here you can see there is an option to use. 32 00:02:34,150 --> 00:02:34,900 ‫Try and catch. 33 00:02:34,900 --> 00:02:44,920 ‫C or I can simply use the try double tap and it's going to create a try catch exception throw for me. 34 00:02:45,100 --> 00:02:53,050 ‫So now what I want to do is I want to try that code and if it didn't work then I don't want my program 35 00:02:53,050 --> 00:02:53,830 ‫to crash. 36 00:02:53,830 --> 00:02:55,780 ‫And that's what this whole try thing is about. 37 00:02:55,780 --> 00:03:03,400 ‫So whatever error, code error in the sense of during runtime error code is in here will be tried to 38 00:03:03,400 --> 00:03:04,000 ‫execute. 39 00:03:04,000 --> 00:03:06,820 ‫And if it didn't work, then the catch. 40 00:03:07,670 --> 00:03:09,340 ‫BLOCK will be executed. 41 00:03:09,350 --> 00:03:14,400 ‫So whatever is in this body or within the curly brackets here is going to be executed. 42 00:03:14,420 --> 00:03:16,250 ‫So now we only have the throw here. 43 00:03:16,250 --> 00:03:19,030 ‫So now let's get rid of throw because it's still throwing. 44 00:03:19,040 --> 00:03:23,990 ‫Yara Now let's write something to the console if an error occurs. 45 00:03:24,140 --> 00:03:28,790 ‫So we say something like format exception. 46 00:03:31,670 --> 00:03:37,760 ‫Please enter the correct type next time. 47 00:03:38,780 --> 00:03:41,300 ‫And that's something that we know what the type is. 48 00:03:41,810 --> 00:03:43,580 ‫A general user would not know that. 49 00:03:43,580 --> 00:03:48,740 ‫So here you would write something like Please don't enter letters, but only numbers or something like 50 00:03:48,740 --> 00:03:49,190 ‫that. 51 00:03:49,280 --> 00:03:51,080 ‫So let's have a look here again. 52 00:03:51,080 --> 00:03:56,120 ‫Let's run an error into an error again, and then we just get this information format an exception. 53 00:03:56,120 --> 00:03:58,340 ‫Please enter the correct type next time. 54 00:03:58,340 --> 00:04:04,700 ‫And our program didn't crash, so we didn't run into this exception in the sense of our program or our 55 00:04:04,700 --> 00:04:10,340 ‫ID telling us, hey, there is something wrong and you should well handle your code, right? 56 00:04:10,490 --> 00:04:13,130 ‫So that's generally how the structure is. 57 00:04:13,130 --> 00:04:18,710 ‫You have a try, so you try some code in here which could run into errors, which can have an exception, 58 00:04:18,710 --> 00:04:23,510 ‫and then you have a catch block which is executed whenever destroy didn't work out. 59 00:04:23,510 --> 00:04:27,680 ‫So whenever there was an error, this catch block is going to be executed. 60 00:04:28,310 --> 00:04:31,190 ‫What we have here is a catch with an exception. 61 00:04:31,190 --> 00:04:34,310 ‫An exception is a very generic error. 62 00:04:34,310 --> 00:04:41,330 ‫So it could be any type of exception, but if we actually run into that error, so let's get rid of 63 00:04:41,330 --> 00:04:46,190 ‫this code, we will see what type of exception we have created. 64 00:04:46,190 --> 00:04:51,440 ‫So I'm just going to enter something like whatever a string I'm entering. 65 00:04:52,660 --> 00:04:55,510 ‫And actually nothing happens because we don't throw. 66 00:04:55,600 --> 00:04:57,520 ‫So let's stop that. 67 00:04:57,550 --> 00:04:59,530 ‫Let's throw as we saw earlier. 68 00:04:59,560 --> 00:05:03,030 ‫Just throw the error and we run it again. 69 00:05:03,040 --> 00:05:07,240 ‫I'm just entering something here and it says system forward exception. 70 00:05:07,240 --> 00:05:11,830 ‫So we have a little more precise information about the actual exception. 71 00:05:11,830 --> 00:05:19,420 ‫So now we know, okay, it is a format exception, so we should use format exception here instead of 72 00:05:19,420 --> 00:05:20,170 ‫exception. 73 00:05:21,340 --> 00:05:22,990 ‫So a lot of exceptions here. 74 00:05:22,990 --> 00:05:25,990 ‫But what we use here is a format exception. 75 00:05:25,990 --> 00:05:31,780 ‫As you can see here, there is a class called system, that format exception and the exception that 76 00:05:31,780 --> 00:05:38,200 ‫is thrown when the format of an argument is invalid or when a composite format string is not well formed. 77 00:05:38,240 --> 00:05:39,880 ‫That's exactly what we have here. 78 00:05:39,880 --> 00:05:43,420 ‫So let's get rid of this throw and. 79 00:05:44,450 --> 00:05:45,490 ‫Be fine with that. 80 00:05:45,500 --> 00:05:47,480 ‫So now let's run it to a different era. 81 00:05:47,750 --> 00:05:50,810 ‫Now let's create a very long number. 82 00:05:51,200 --> 00:05:54,080 ‫Something that cannot fit into a string. 83 00:05:54,350 --> 00:06:00,250 ‫Because, as you might guess, or as you might remember, a string only has a specific amount of space. 84 00:06:00,260 --> 00:06:04,820 ‫So let's say I'm not sure how many numbers does this, but let's say only this amount of numbers can 85 00:06:04,820 --> 00:06:06,440 ‫be stored within an integer. 86 00:06:06,440 --> 00:06:10,700 ‫So all of this here is not going to work because it's an overflow. 87 00:06:10,700 --> 00:06:13,310 ‫So it's too much information for a string to handle. 88 00:06:13,310 --> 00:06:14,810 ‫So if I enter this now. 89 00:06:18,520 --> 00:06:22,150 ‫I get an input system overflow exception. 90 00:06:22,150 --> 00:06:22,900 ‫So here we are. 91 00:06:22,900 --> 00:06:24,850 ‫System overflow exception. 92 00:06:24,850 --> 00:06:30,340 ‫Well, you was either too large or too small for an RN 32. 93 00:06:30,970 --> 00:06:31,300 ‫All right. 94 00:06:31,300 --> 00:06:32,440 ‫That's what I said. 95 00:06:32,470 --> 00:06:35,710 ‫And now we need to catch that exception as well. 96 00:06:35,920 --> 00:06:39,070 ‫So there are two ways of how we could handle it. 97 00:06:39,070 --> 00:06:43,900 ‫So either we use a different exception here and we just use exception. 98 00:06:43,900 --> 00:06:52,270 ‫So we're just handling all other exceptions that there are generically and we simply say console dot, 99 00:06:52,300 --> 00:06:53,350 ‫red line, 100 00:06:55,810 --> 00:07:03,430 ‫general exception, something went wrong, for example. 101 00:07:03,430 --> 00:07:10,240 ‫So let's create a very long number actually once again like this and let's enter that. 102 00:07:10,240 --> 00:07:14,770 ‫And we see we have a general exception, even though that's a overflow exception. 103 00:07:14,770 --> 00:07:16,530 ‫So you can handle it that way. 104 00:07:16,540 --> 00:07:20,380 ‫So if you don't know all the different kinds of exceptions that you have there or you don't want to 105 00:07:20,380 --> 00:07:27,490 ‫catch them all individually, you could do this way or you could be that precise and simply enter the 106 00:07:27,490 --> 00:07:29,650 ‫right one overflow exception. 107 00:07:30,280 --> 00:07:33,370 ‫So now it's going to work and you see a system overflow exception. 108 00:07:33,370 --> 00:07:39,610 ‫Let's run that again and let's please enter a number and let's create a very long number here. 109 00:07:39,940 --> 00:07:47,020 ‫And we still get the general exception because we didn't enter a different text overflow exception. 110 00:07:47,830 --> 00:07:54,820 ‫The number was too long or too short for an in 32. 111 00:07:56,510 --> 00:07:58,160 ‫So let's have a look again. 112 00:08:00,350 --> 00:08:05,120 ‫And overflow exception number was too long or too short for in 32. 113 00:08:06,470 --> 00:08:07,130 ‫All right. 114 00:08:07,250 --> 00:08:10,610 ‫So this is how we can generally catch exceptions. 115 00:08:10,610 --> 00:08:16,690 ‫And if there are any other exceptions that can run, as you can see, there is an argument null exception. 116 00:08:16,700 --> 00:08:20,060 ‫So let's have a look at what that means. 117 00:08:20,090 --> 00:08:22,880 ‫If we can even get that and we see. 118 00:08:24,210 --> 00:08:24,900 ‫An exception. 119 00:08:24,900 --> 00:08:27,900 ‫Please enter the correct type of type next time. 120 00:08:27,900 --> 00:08:34,380 ‫So even if it's empty, in our case, we don't get this null exception because our input, even if we 121 00:08:34,380 --> 00:08:39,150 ‫don't enter anything, still has an empty string which is not null. 122 00:08:39,360 --> 00:08:42,750 ‫So we don't run in this according to this actual exception. 123 00:08:42,750 --> 00:08:49,350 ‫So as you see here, whenever you try to execute some code, Intel is even giving you some information 124 00:08:49,350 --> 00:08:49,530 ‫here. 125 00:08:49,530 --> 00:08:50,670 ‫So that's really useful. 126 00:08:50,670 --> 00:08:54,960 ‫You can see there is the argument null exception, the format exception and the overflow exception. 127 00:08:54,960 --> 00:09:04,080 ‫So we could even add another exception here and call that one argument is null exception or argument 128 00:09:04,080 --> 00:09:04,950 ‫null exception. 129 00:09:04,950 --> 00:09:08,850 ‫And we say something like argument null exception. 130 00:09:10,700 --> 00:09:16,550 ‫The value was empty now. 131 00:09:16,670 --> 00:09:22,790 ‫So in order to even run into this era, we would have to use nulls, which we haven't covered yet, 132 00:09:22,790 --> 00:09:24,710 ‫but we will do so later on. 133 00:09:24,860 --> 00:09:27,860 ‫And if we have them, you can try to do that. 134 00:09:27,860 --> 00:09:33,050 ‫So if you have seen the notable video already, you could try to run into that error. 135 00:09:33,470 --> 00:09:33,950 ‫All right. 136 00:09:33,950 --> 00:09:36,800 ‫So now you have seen how to use try catch. 137 00:09:36,800 --> 00:09:43,070 ‫And now there is one more thing that I would like to show you in this video, which is finally and finally 138 00:09:43,730 --> 00:09:44,930 ‫is done like that. 139 00:09:44,930 --> 00:09:53,300 ‫So you used the final keyword and then you use curly brackets again and within that code block here, 140 00:09:53,300 --> 00:09:59,830 ‫so within that body, you enter the code that should be executed whenever. 141 00:10:00,620 --> 00:10:02,660 ‫Try and catch our done so. 142 00:10:02,660 --> 00:10:06,920 ‫It doesn't matter if the error was run or if the error didn't work. 143 00:10:06,920 --> 00:10:07,970 ‫There was no error. 144 00:10:08,420 --> 00:10:10,130 ‫Finally, we'll still be called. 145 00:10:10,130 --> 00:10:14,960 ‫So this is called anyways. 146 00:10:17,330 --> 00:10:19,760 ‫So that means as well. 147 00:10:19,760 --> 00:10:20,870 ‫We will see in a second. 148 00:10:20,870 --> 00:10:25,940 ‫So let's run into an arrow and we see we have a four minute exception and then we see this is called 149 00:10:25,940 --> 00:10:28,880 ‫anyways and that's our final block. 150 00:10:29,210 --> 00:10:36,350 ‫And the idea behind this finally block is to, for example, if you tried to start an Internet connection 151 00:10:36,350 --> 00:10:41,810 ‫or if you try to write into a file, there eventually can happen in error. 152 00:10:41,810 --> 00:10:47,120 ‫So let's say the Internet connection breaks or you don't have access to a file that you want to write 153 00:10:47,120 --> 00:10:57,410 ‫into or some other exceptions that can come up and you want to close the connection or the file either 154 00:10:57,410 --> 00:10:57,770 ‫way. 155 00:10:57,770 --> 00:11:04,550 ‫So you have open to file and now you try to write into that file and after you have written into that 156 00:11:04,550 --> 00:11:09,410 ‫file within the try block and maybe it worked, maybe it didn't work, but what you definitely want 157 00:11:09,410 --> 00:11:15,830 ‫to do is to close the file again, or you try to download something from the internet and if it worked 158 00:11:15,830 --> 00:11:17,210 ‫or it didn't work, doesn't matter. 159 00:11:17,210 --> 00:11:21,710 ‫You need to close the connection to the Internet afterwards, and that's something that you would do 160 00:11:21,710 --> 00:11:22,820 ‫in the file. 161 00:11:22,820 --> 00:11:31,430 ‫BLOCK And that's very handy and quite important when working with input output types or yeah, generally 162 00:11:31,430 --> 00:11:36,020 ‫with connections to Internet, Bluetooth or whatever. 163 00:11:36,380 --> 00:11:43,100 ‫So as you see, there is a lot to know about try and catch, but the most important things you have 164 00:11:43,100 --> 00:11:43,700 ‫seen already. 165 00:11:43,700 --> 00:11:48,740 ‫So now you know how to use try you know how to use, catch how to find out exceptions. 166 00:11:48,740 --> 00:11:50,750 ‫In some cases it's not that obvious. 167 00:11:50,750 --> 00:11:54,770 ‫So in some cases you won't check which kind of exceptions that can be run. 168 00:11:54,770 --> 00:12:00,320 ‫But if you run into an exception in your code, then you know which kind of exception that is because 169 00:12:00,320 --> 00:12:05,270 ‫well, the idea is so Microsoft Visual Studio will tell you which exception it was and then you definitely 170 00:12:05,270 --> 00:12:12,490 ‫should try to catch that exception or just use a general exception type okeydokey. 171 00:12:12,500 --> 00:12:19,040 ‫So now is a little challenge for you because there always has to be a little challenge, otherwise you 172 00:12:19,040 --> 00:12:24,500 ‫might get a little, well, bored or you might get a little lazy. 173 00:12:24,500 --> 00:12:28,520 ‫It's really important that you try to type the code as well and check it out for yourself. 174 00:12:28,520 --> 00:12:33,200 ‫So the little challenge is please go ahead and divide by zero. 175 00:12:33,740 --> 00:12:34,550 ‫So that's simple. 176 00:12:34,550 --> 00:12:39,140 ‫Just try to divide by zero and use two variables for that. 177 00:12:41,290 --> 00:12:44,980 ‫All right, let's go ahead and try just that. 178 00:12:44,980 --> 00:12:49,900 ‫So I'm going to try to divide by zero. 179 00:12:49,900 --> 00:12:52,650 ‫I'm going to use two integers and none. 180 00:12:52,660 --> 00:12:59,320 ‫One is going to be five and n none two is going to be zero. 181 00:13:00,700 --> 00:13:08,350 ‫And now I'm going to use a resultant result is going to be entered here. 182 00:13:08,350 --> 00:13:13,000 ‫Result is equal to num one divided by num two. 183 00:13:13,000 --> 00:13:17,980 ‫So now let's try that without try and catch actually before we use try and catch. 184 00:13:18,430 --> 00:13:19,690 ‫So let's run it. 185 00:13:22,170 --> 00:13:28,140 ‫Please enter a number and then divide by zero exception, and I hope you handle that. 186 00:13:28,260 --> 00:13:30,450 ‫So now let's add a try and catch. 187 00:13:30,870 --> 00:13:36,500 ‫So try or actually just make it easier. 188 00:13:36,510 --> 00:13:37,080 ‫Let's enter. 189 00:13:37,080 --> 00:13:41,040 ‫Try here and use the block double tab. 190 00:13:41,370 --> 00:13:44,280 ‫And now within the try, I want to run that. 191 00:13:44,370 --> 00:13:50,580 ‫And if it didn't work, I simply want to write something like C double. 192 00:13:50,580 --> 00:13:54,930 ‫You can't divide by zero. 193 00:13:55,350 --> 00:13:55,940 ‫And now this. 194 00:13:56,010 --> 00:13:59,610 ‫The exception that we just had was so we'll look. 195 00:14:01,580 --> 00:14:06,590 ‫Divide by zero attempt to divide by zero, and it's called divide by zero exception. 196 00:14:06,590 --> 00:14:09,830 ‫So if you want to find that, you can find that down here. 197 00:14:09,830 --> 00:14:11,840 ‫So I'm just going to use exactly that. 198 00:14:11,840 --> 00:14:15,350 ‫I'm going to replace exception with divide by zero exception. 199 00:14:15,680 --> 00:14:17,810 ‫So now let's run that code again. 200 00:14:18,710 --> 00:14:23,000 ‫Please enter number that was from before and then it says can divide by zero. 201 00:14:24,170 --> 00:14:24,800 ‫Right. 202 00:14:24,800 --> 00:14:29,030 ‫So I hope you try that and I hope you achieve to do just that. 203 00:14:29,030 --> 00:14:33,440 ‫And you now can see how you can create your own try and catch this. 204 00:14:33,680 --> 00:14:37,760 ‫And in the next video we're going to go ahead and use some more fancy stuff. 205 00:14:37,760 --> 00:14:38,660 ‫So see you there.