1 00:00:00,120 --> 00:00:05,490 Welcome back in this video, we are going to check out exception handling, which means the handling 2 00:00:05,490 --> 00:00:11,220 of runtime problems which occur in the program and what otherwise leads to program termination. 3 00:00:11,520 --> 00:00:15,300 So this is the case for not only apps, but for all programs. 4 00:00:15,450 --> 00:00:16,530 So what is an exception? 5 00:00:16,950 --> 00:00:23,280 An exception is a runtime problem which occurs in the program and leads to a program termination, which 6 00:00:23,370 --> 00:00:27,090 on the phone means that the app is not going to respond. 7 00:00:27,880 --> 00:00:30,810 Now that is a very, very bad behavior. 8 00:00:30,810 --> 00:00:34,650 So you should at all costs, try to avoid exceptions to not be handled. 9 00:00:34,920 --> 00:00:36,480 So just to just run. 10 00:00:37,080 --> 00:00:44,040 So examples would be running out of memory array out of bounds condition like divide by zero. 11 00:00:44,610 --> 00:00:47,490 And you handle this type of problem during program execution. 12 00:00:47,490 --> 00:00:52,650 The technique of exception handling is used and exception handling is a technique which handles the 13 00:00:52,650 --> 00:00:56,790 runtime problems and maintains the flow of the program execution. 14 00:00:57,030 --> 00:01:01,050 So when you use exception, handling the exceptions may still arise. 15 00:01:01,050 --> 00:01:05,880 But they will not make your program crash and your program will still work, even though it might not 16 00:01:05,880 --> 00:01:07,590 do the things as intended. 17 00:01:07,620 --> 00:01:13,980 For example, if you try to access the internet and you don't handle the exception that the internet 18 00:01:13,980 --> 00:01:19,260 connection doesn't work, then you would run into an error and you would get an exception because you 19 00:01:19,260 --> 00:01:24,690 can't connect to the internet and that could break your app if you don't handle this exception. 20 00:01:24,990 --> 00:01:30,780 That's why it's super important to handle those kind of exceptions and yet still make your program work. 21 00:01:31,080 --> 00:01:34,590 So even if the internet doesn't work, then you will just display something to the user. 22 00:01:34,830 --> 00:01:38,760 Please turn on your internet or get an internet connection or something like that. 23 00:01:39,870 --> 00:01:40,260 All right. 24 00:01:40,680 --> 00:01:45,870 So there is a third world class, which allows us to throw an exception. 25 00:01:46,260 --> 00:01:52,140 So this throws an exception if we're used as through keyword and then the name of the exception, I 26 00:01:52,140 --> 00:01:57,750 call this exception my exception, because we can create our own types of exceptions, which will then 27 00:01:57,750 --> 00:02:00,540 do whatever we want to do in those cases. 28 00:02:01,140 --> 00:02:03,620 There are four different keywords used an exception handling. 29 00:02:03,690 --> 00:02:07,500 And these are try, catch finally and throw. 30 00:02:08,009 --> 00:02:11,580 So these are the main keywords, and we're going to check those keywords out. 31 00:02:12,420 --> 00:02:14,040 So keywords used an exception handling. 32 00:02:14,070 --> 00:02:16,320 Let's look at the try keyword to try. 33 00:02:16,320 --> 00:02:19,980 Block contains a set of statements which might generate an exception. 34 00:02:20,310 --> 00:02:23,880 It must be followed by either catch or finally or both. 35 00:02:24,420 --> 00:02:24,780 All right. 36 00:02:24,780 --> 00:02:32,160 So this is the code that can break, that can make and more create an error, create an exception such 37 00:02:32,160 --> 00:02:36,180 as connected to the internet or running through an array. 38 00:02:36,420 --> 00:02:38,940 So that can be a lot of issues that come up. 39 00:02:39,810 --> 00:02:44,540 Then the catch block is used to catch deception thrown from the tribe. 40 00:02:44,940 --> 00:02:49,650 So if there is an exception in the tribe block, the catch block should execute. 41 00:02:50,190 --> 00:02:51,600 Otherwise, it will not execute. 42 00:02:51,630 --> 00:02:53,420 So if there's no problem in the tribe lock. 43 00:02:53,460 --> 00:02:58,710 So if the code works perfectly fine, for example, back to our internet problem, let's say we have 44 00:02:58,710 --> 00:03:04,380 internet and we try to exit the connection, then we will not have a problem. 45 00:03:04,620 --> 00:03:07,350 The try block will work, the catch block will not execute. 46 00:03:07,500 --> 00:03:11,760 But if the internet connection is not there, then of course the catch block should run. 47 00:03:12,300 --> 00:03:12,810 Then we have. 48 00:03:12,810 --> 00:03:18,530 Finally, the final block allows execution, whether exception is handled or not. 49 00:03:18,540 --> 00:03:24,150 So it is used to execute important code statements like closing buffers, for example, closing a buffer 50 00:03:24,150 --> 00:03:31,020 which takes care of the connection to the internet, or a buffer that takes care of storing data on 51 00:03:31,020 --> 00:03:31,470 the phone. 52 00:03:31,830 --> 00:03:38,730 So these are things that need to be closed after they have been opened and that is usually done. 53 00:03:39,120 --> 00:03:44,640 And finally, so after the tribe lock has been executed or after the catch block has executed. 54 00:03:45,000 --> 00:03:51,360 If there was an error and then we have this through keyword, which allows us to throw an exception 55 00:03:51,360 --> 00:03:58,320 explicitly so we ourselves write this in order to throw an exception in order to cause an error. 56 00:03:58,660 --> 00:04:06,450 And this is especially useful when testing to see where an error occurs and we create our own exceptions 57 00:04:06,450 --> 00:04:12,230 or throws so that we know, OK, this is something that we just wrote, All right. 58 00:04:12,240 --> 00:04:16,050 And then there you are, sitting in front of your code and suddenly an exception arises. 59 00:04:16,170 --> 00:04:21,230 Well, the thing is that usually happens when you have unchecked exceptions. 60 00:04:21,570 --> 00:04:27,180 So an unchecked exception is the exception, which is thrown due to mistakes in our code. 61 00:04:27,690 --> 00:04:35,220 So our code for some reason doesn't work, and there are multiple different exceptions, and the exceptions 62 00:04:35,220 --> 00:04:39,660 don't run during runtime or execute during runtime or runtime exceptions. 63 00:04:39,660 --> 00:04:42,660 And they extend from this runtime exception. 64 00:04:42,660 --> 00:04:50,010 Class of the unchecked exception is checked at runtime, which means when our app is running and not 65 00:04:50,010 --> 00:04:55,290 during compile time, meaning when we compile our app and press, for example, the run button. 66 00:04:56,850 --> 00:05:02,790 Examples of unchecked exceptions are arithmetic exceptions thrown, wouldn't we divide a number by zero, 67 00:05:02,790 --> 00:05:10,170 for example, then the right index out of bond exception is thrown when an array has been tried to access 68 00:05:10,170 --> 00:05:11,940 with incorrect index value. 69 00:05:11,950 --> 00:05:16,950 So let's say the array only has five values, but you're trying to access to sixth or seventh value. 70 00:05:17,280 --> 00:05:18,600 Then you get this error. 71 00:05:18,960 --> 00:05:21,960 You can very easily try that and force this kind of an error. 72 00:05:22,170 --> 00:05:23,730 And you probably have seen this already. 73 00:05:24,420 --> 00:05:31,110 And then there's security exception thrown by the security manager to indicate that we, yeah, have 74 00:05:31,110 --> 00:05:32,280 a security violation. 75 00:05:32,280 --> 00:05:39,330 So for example, we're trying to access GPS, but we don't have the rights to access GPS because the 76 00:05:39,330 --> 00:05:43,020 user didn't give us the rights to use the aGPS functionality. 77 00:05:43,440 --> 00:05:50,130 And then the very common null pointer exception thrown when invoking a method or property on a null 78 00:05:50,130 --> 00:05:50,610 object. 79 00:05:50,790 --> 00:05:58,230 So when we tried to access an object which is empty, which is now, then we get this null pointer exception. 80 00:05:58,620 --> 00:06:03,180 And that is something that I have already talked about when we looked at numbers. 81 00:06:04,760 --> 00:06:05,840 Then checked exceptions. 82 00:06:05,990 --> 00:06:11,930 Well, they are acceptance, which are checked at compile time, so pretty much in our code, we write 83 00:06:11,930 --> 00:06:12,650 this ourselves. 84 00:06:13,130 --> 00:06:18,320 There's exception type extends the throw bill class, so there's a class control bill, which takes 85 00:06:18,320 --> 00:06:20,630 care of the exception. 86 00:06:20,630 --> 00:06:26,050 Functionality and examples of exceptions would be the input output exception. 87 00:06:26,060 --> 00:06:29,660 So i o exception or the sequel exception? 88 00:06:29,780 --> 00:06:31,250 And there are a bunch more. 89 00:06:31,910 --> 00:06:35,900 Now let's look at the try catch block, which is used for exception handling in the code. 90 00:06:36,380 --> 00:06:39,950 The syntax of try with catch block looks like this. 91 00:06:40,070 --> 00:06:45,170 So we have this try then code that may throw the exception and then we have this catch. 92 00:06:45,410 --> 00:06:47,720 For example, some exceptions. 93 00:06:47,720 --> 00:06:53,570 So this could be the Iowa exception if it's an input output exception, or it could be just a generic 94 00:06:53,810 --> 00:06:54,350 exception. 95 00:06:54,350 --> 00:06:57,410 So we would just use the generic exception clause name here. 96 00:06:57,740 --> 00:07:00,450 And then we have our code that handles the exception. 97 00:07:00,470 --> 00:07:03,590 And of course, we need to close the bracket here. 98 00:07:04,190 --> 00:07:04,490 All right. 99 00:07:04,530 --> 00:07:06,650 Examples without exception handling. 100 00:07:07,160 --> 00:07:11,610 So in this case, we have an exception handling Britain ourselves. 101 00:07:11,610 --> 00:07:12,530 So try and catch. 102 00:07:12,530 --> 00:07:16,760 But the exception is never arising because the problem doesn't appear. 103 00:07:17,060 --> 00:07:20,180 So here, for example, we use value string get. 104 00:07:20,180 --> 00:07:20,780 No. 105 00:07:21,050 --> 00:07:25,250 So we use this function, get no, and we have a value of tender. 106 00:07:26,030 --> 00:07:32,180 So the variable string is getting the value of 10 and then making a string out of it. 107 00:07:32,630 --> 00:07:36,000 So we're trying to integer parse it. 108 00:07:36,020 --> 00:07:39,980 So we're trying to parse that value into a string. 109 00:07:40,040 --> 00:07:41,260 So it's an integer, right? 110 00:07:41,300 --> 00:07:41,600 10. 111 00:07:41,810 --> 00:07:44,150 But we're making a string out of it. 112 00:07:44,660 --> 00:07:50,810 And if something goes wrong, for example, an arithmetic exception appears, then we want to output 113 00:07:50,810 --> 00:07:51,200 zero. 114 00:07:51,650 --> 00:07:52,010 All right. 115 00:07:52,010 --> 00:07:55,970 So if we run print and string now we get the output of 10. 116 00:07:56,720 --> 00:08:01,960 All right, now, let's look at the exception handling when it's actually running. 117 00:08:01,970 --> 00:08:06,410 So when we actually do have an exception handling running, and that is the case, for example, if 118 00:08:06,410 --> 00:08:11,960 we try to have a parsing here with a value of ten point five. 119 00:08:12,260 --> 00:08:15,710 So in this case, we're trying to parse a double as an integer, and that doesn't work. 120 00:08:15,950 --> 00:08:18,300 So we have to be careful with the type here, of course. 121 00:08:18,620 --> 00:08:20,610 And that creates an error. 122 00:08:20,630 --> 00:08:25,580 So our try block does actually create an arithmetic exception. 123 00:08:26,000 --> 00:08:27,290 So the output is zero. 124 00:08:27,740 --> 00:08:29,840 So whatever we have in our catch block here. 125 00:08:30,910 --> 00:08:31,330 All right. 126 00:08:31,870 --> 00:08:35,030 So that is something that we, of course, have to be careful with. 127 00:08:35,530 --> 00:08:40,299 Then we can have multiple catch blocks so we can use multiple catch blocks in our code. 128 00:08:40,720 --> 00:08:47,560 And they are used when we can have different types of operations or have different types of operation 129 00:08:47,560 --> 00:08:51,190 errors in a tri block, which may cause different exceptions in the tribal. 130 00:08:51,400 --> 00:08:52,880 So here's an example. 131 00:08:53,200 --> 00:08:56,500 We have this function where we try. 132 00:08:57,670 --> 00:09:05,620 To get the value a out of an interim, which is five, so which has five elements in there. 133 00:09:05,980 --> 00:09:13,000 And now we're saying a at position five, which are at Index five, which means at its sixth position 134 00:09:13,510 --> 00:09:15,430 should be 10, divided by zero. 135 00:09:15,820 --> 00:09:18,400 So there are a bunch of problems that we create here. 136 00:09:18,610 --> 00:09:22,900 One of them being that 10 cannot be or anything cannot be divided by zero. 137 00:09:22,930 --> 00:09:27,910 So we get and I really the exception here trying to divide by zero. 138 00:09:28,240 --> 00:09:30,280 Not good doesn't work creates an error. 139 00:09:30,640 --> 00:09:35,890 And then we're trying to access an array at a position which doesn't exist. 140 00:09:35,950 --> 00:09:39,970 So the array just has five values, but we're trying to access that sixth value. 141 00:09:40,180 --> 00:09:42,250 So we're creating this problem here. 142 00:09:42,820 --> 00:09:46,150 So here this is where the problem arises. 143 00:09:46,660 --> 00:09:47,030 All right. 144 00:09:47,470 --> 00:09:53,680 So we have two arrows that can appear here, and then we have the arithmetic exception, which takes 145 00:09:53,680 --> 00:09:58,270 care of arithmetic more than we'll just print of my take exception catch. 146 00:09:58,570 --> 00:10:04,000 So, for example, trying to divide by zero, then we have an array index out of bounds exception, 147 00:10:04,000 --> 00:10:09,190 which will be run because of this, a accessing index five. 148 00:10:10,030 --> 00:10:14,380 And then we have a generic exception, which is the apparent exception clause. 149 00:10:14,380 --> 00:10:16,900 So it's just the name exception. 150 00:10:16,990 --> 00:10:22,300 So you can see in catch, we always have this e colon and then the name of the exception that we want 151 00:10:22,300 --> 00:10:22,690 to use. 152 00:10:23,140 --> 00:10:27,690 And by the way, you can get information about the arrow. 153 00:10:27,700 --> 00:10:34,240 So a little more information or pretty much so a lot of information when you just print the stack trace 154 00:10:34,240 --> 00:10:34,690 of it. 155 00:10:35,620 --> 00:10:36,010 All right. 156 00:10:36,010 --> 00:10:43,570 So we can see that the output here will be an arithmetic exception catch. 157 00:10:44,200 --> 00:10:46,750 And then we have the code after the try catch. 158 00:10:47,140 --> 00:10:52,690 So we can also have nested try catch blocks and that is sometimes required. 159 00:10:52,690 --> 00:10:58,270 But the requirement of this to try catch blocks arises when a block of code generates an exception and 160 00:10:58,270 --> 00:10:59,020 within a block. 161 00:10:59,020 --> 00:11:02,380 Another code statement also generates an exception. 162 00:11:02,650 --> 00:11:06,610 So that is the exception inception, so to speak. 163 00:11:07,390 --> 00:11:07,720 All right. 164 00:11:07,720 --> 00:11:09,610 Syntax of nested try blocks. 165 00:11:09,610 --> 00:11:10,450 Let's look at this. 166 00:11:10,480 --> 00:11:15,370 So we have a try code block and then we have another try code block and then we have the catch block 167 00:11:15,370 --> 00:11:22,030 for the first try and then add some exception code that we have, and then we have another exception 168 00:11:22,210 --> 00:11:23,200 that we catch. 169 00:11:23,350 --> 00:11:27,600 So as you can see, the try has to be followed by a catch. 170 00:11:27,610 --> 00:11:33,810 But when you have a try inside of a try, that's try has to be followed by a catch. 171 00:11:33,820 --> 00:11:36,340 OK, so these two are connected with each other. 172 00:11:36,610 --> 00:11:39,640 And this try here is connected to that Cacho down there. 173 00:11:41,530 --> 00:11:41,950 All right. 174 00:11:42,370 --> 00:11:43,840 So this is an example for that. 175 00:11:43,930 --> 00:11:49,270 Then we have the final block and you saw the keyword finally before and to finally block is such a block 176 00:11:49,270 --> 00:11:53,170 which is always executed, whether exception is handled or not. 177 00:11:53,380 --> 00:11:56,470 So it is used to execute important code statements. 178 00:11:57,130 --> 00:12:02,890 For example, as I said, earlier code that should be run when all of the other code just done, for 179 00:12:02,890 --> 00:12:05,140 example, closing the connection again. 180 00:12:05,500 --> 00:12:11,620 So we have this function main here where we try to divide the time by five and then we print the data. 181 00:12:11,800 --> 00:12:16,150 So our catch that we have here is a no pointer exception catch. 182 00:12:16,360 --> 00:12:16,780 So. 183 00:12:17,630 --> 00:12:23,450 We have this exception that we pointed here because data could be empty, for example, and then we 184 00:12:23,450 --> 00:12:30,410 have this finally block here, which always executes and then we have a print line below code just to 185 00:12:30,470 --> 00:12:31,640 see what's executed. 186 00:12:31,880 --> 00:12:37,790 And if we run this code, we will see that to then finally, block always executes and below code are 187 00:12:37,790 --> 00:12:44,760 printed onto our console, and that has to do with first of all, ten divided by five is two. 188 00:12:44,780 --> 00:12:46,730 So this print statement is printed. 189 00:12:47,240 --> 00:12:50,930 Then here you see we print the EA, so the null pointer exception. 190 00:12:51,320 --> 00:12:53,720 So if that would arise, we would print the whole exception. 191 00:12:54,380 --> 00:13:01,280 And then we have this final block, which is executed no matter if try is running without an error or 192 00:13:01,670 --> 00:13:05,540 if it's run with with an error, so the catch block will be run as well. 193 00:13:06,170 --> 00:13:08,390 So either way, we run our finally. 194 00:13:08,390 --> 00:13:13,070 So our print to finally block always executes is executed. 195 00:13:13,070 --> 00:13:15,650 And of course, our below code is also executed. 196 00:13:16,580 --> 00:13:22,610 Then we have this throw key and throw keyword is used to throw an explicit exception, and it's used 197 00:13:22,610 --> 00:13:26,690 to throw a custom exception, for example, to throw an exception object. 198 00:13:27,020 --> 00:13:32,510 We will use the throw exception, so the syntax of the keyword is throw some exception. 199 00:13:33,810 --> 00:13:39,900 And let's have a look at for this or we have this function where we use this validate function and we 200 00:13:39,900 --> 00:13:44,850 add the value of 15 in there, or we pass a value of 15 and we print some code after the validation 201 00:13:44,850 --> 00:13:48,150 check or pretty much after the validate function. 202 00:13:48,720 --> 00:13:53,880 So in the validate function, we check if the age is below 18 and if that's the case, then we throw 203 00:13:53,880 --> 00:13:56,910 an arithmetic exception where we say under h. 204 00:13:57,300 --> 00:14:02,310 And if the age is not under 18, then we say eligible for drive. 205 00:14:02,520 --> 00:14:06,900 So this is not the case for the US, but it's the case for many other countries where you have to be 206 00:14:06,900 --> 00:14:08,010 18 in order to drive. 207 00:14:08,280 --> 00:14:10,140 So the output here is exception. 208 00:14:10,140 --> 00:14:15,080 Infrared main java the length that arithmetic exception colon under age. 209 00:14:15,090 --> 00:14:20,520 So after the column comes, whatever we have entered in those brackets. 210 00:14:20,700 --> 00:14:21,170 OK. 211 00:14:21,300 --> 00:14:22,950 So in our case, under age. 212 00:14:24,020 --> 00:14:30,680 So this is something that comes in handy when you actually want to make sure that you check your code 213 00:14:30,680 --> 00:14:38,300 correctly and you just want to throw these kind of errors in order to break your code in order to understand 214 00:14:38,300 --> 00:14:39,860 where the problems arise. 215 00:14:40,160 --> 00:14:45,470 So in this case, it wouldn't make sense because it's a dumb example, so to speak. 216 00:14:45,470 --> 00:14:50,990 But in real life, it would make a lot more sense when you, for example, need to check your app. 217 00:14:52,090 --> 00:14:58,390 For potential incorrect entries that users can make or for potential problems that arise when you get 218 00:14:58,390 --> 00:15:00,790 data from the web and stuff like that. 219 00:15:01,740 --> 00:15:06,600 And you don't want to publish the app with a lot of throw statements or a lot of errors. 220 00:15:07,000 --> 00:15:10,050 Okay, because a throw statement actually makes your app crush. 221 00:15:11,160 --> 00:15:11,560 All right. 222 00:15:11,580 --> 00:15:12,720 Now let's get back to coding. 223 00:15:12,750 --> 00:15:18,780 This was a super long example of the whole concepts of try and catch blocks, but now you know pretty 224 00:15:18,780 --> 00:15:19,800 much everything about it. 225 00:15:20,070 --> 00:15:21,990 And we can use it in our code. 226 00:15:22,230 --> 00:15:28,290 And all of this might look a little intimidating, but it would really become your second nature once 227 00:15:28,290 --> 00:15:31,650 you use it and you see what the advantages of all of those things are. 228 00:15:32,250 --> 00:15:35,610 So I think let's get back to coding and see in the next video.