Java Exceptions - Learn Exceptions in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you may have heard the word exception and then wanted to stop programming right there because teachers do a horrible job explaining what exceptions are and you're sitting there like well okay I guess I'll try to figure out on my own and it stays unclear and it's never taught to you exactly what exceptions are so that's what I'm gonna do today so here's my video on exceptions in Java explained for you hey what is up I'm Alex welcome back to another job video on this channel I make Java tutorials just like this one every single week so if you're new here if that's something you might be interested in then consider hitting that subscribe button so I think the best way to learn about exceptions in Java is to actually make some ourselves and try to make some try to make some understanding of it so we'll first make a new java project by going to file new Java project I'll call it just like exceptions in Java hit finish open up that project we just created right click on source and go to new class this is just a Java file naming exceptions shins man Taipings hard Spelling's hard if you're a bad speller leave in the comments because I know I am I have the dictionary definition of an exception in Java so I'm just gonna pull that up right now okay definition an exception is an event which occurs during the execution of a program that disrupts the normal flow of the program instructions okay so an exception is an event that happens when we try to run a program that makes the program stop working so let's make that even more clear by making some exceptions ourself let's make like an array here and try to do something weird with the array so we'll just think like a string array and we'll call it pets equals a new array like that and we'll put in like three pets here like a dog a cat and kangaroo actually kangaroos are pretty scary I wouldn't want one as a pet but what I would want is a monkey let's print something out that we know works we know that we can get an element from an array by typing the name of the array square brackets and then the index which starts at zero so if we wanted to get dog we would type pets zero so save it and run it and we get a dog print it out but what if we forgot that index is started at zero and we wanted a monkey and we when we thought logically well one two three I'll get the third element in type a three in here let's see what happens save it and run it and you get this error with like all these weird words these weird like dots and quotation marks and underlines and parentheses and the first word is exception when I clicked the Run button it tried to make sense of this Java code we just wrote but it noticed that pets three isn't doesn't exist it only goes zero one two there isn't there isn't a fourth one for index three so Java was like nope we can't turn this into machine code we just can't because you're not following the rules so this event stopped the execution of the program and hence we have the word exception here then it continues to say in thread main that just means this this exception was found in the main method then we see this java.lang dot array index out of bounds exception three at exceptions main the exception was thrown in the main method and this is the name of the exception here array index out of bounds exception and it's kind of what what happened here we have an array and an index that's out of the bounds of the array 0 1 2 when we put a 3 there and that's what they show us here that the three that we put in if we try to do I get the six index for whatever reason and ran it we'd get a six shown with that array index out of bounds exception and the second line right here is just saying exceptions main that's our class exceptions dot main main method it's at line seven line seven here is where the exception was and that's great and all but let's keep trying to destroy a program and learn more about exceptions in Java let's say we have an integer I but we set it equal to something in double quotes like like money we get an underline here saying that you can't convert from string to int because string is double quotes and it's an integer variable you just can't do that what would happen if we tried to run it let's save it and click the Run button and just see what happens it's warning us that there's errors but we're just gonna see what happens we get a red message it looks bad and the first word again is exception in thread main main method Java dot lang error is the looks like that's what the exception is and then it goes on to explain it can't compile because there's a type mismatch we can't convert from string Alex to integer in store I and again it shows us exceptions that main and it's on the sixth line so this is very useful for debugging you can look at the exception and it kind of gives you a nice description of kind of what happened what it thinks might have happened to make the program stop working it gives you a line number shows exactly where it's going wrong and so you can read the exception in Java to sort of fix your program there pretty useful and it's it's pretty nice but it looks really confusing at first and they never taught me this in school God look I have my I have my computer science degree right here you see it computer science and they never taught me what exceptions were really they may have talked to sentence or two about it maybe one slide maybe way let's keep let's keep doing some stuff I like to keep you entertained let's try to make an ArrayList see what we can do with array lists I have a video covering ArrayList in Java on the screen now if you want to check that out but we'll just call it like list equals new ArrayList just like that the ArrayList is gonna hold strings so that's why we have these these weird little brackets with strings very special and weird-looking for array lists but we're just going to accept it click import ArrayList to let Java know that we want to use it will type list dot to see what our ArrayList can do and we'll let's try to get another index so we'll get like zero get the first element of this array list but there's nothing in it so is something gonna happen well let's see we'll save it run it and yes something did happen we get another exception most of the red lines like a bunch of lines like this you see are gonna be exceptions and again an exception in Java is just an event that happens when you click the Run button that changes the flow of the program makes it stop working exception in thread main java.lang dot index out of bounds exception this is similar to the array index out of bounds but it's just like it's slightly different the next line it looks like it goes into the ArrayList class itself the ArrayList code itself there's a line in there that went wrong which was linked to another line in ArrayList that went wrong and it all started at exceptions main on line eight right here now I'm gonna sneak peek into one of the most commonly confusing exceptions the null pointer exception and I might have a separate video just on the null pointer exception but let's sort of see what happens what happens is you create an object or a variable and you set it equal to null null just means nothing it doesn't mean 0 it doesn't mean an ArrayList with 0 things in it it just means that this list exists but it's really nothing there it's kind of like a placeholder for the list but there's nothing in it a little tricky but I'll explain that in the null pointer exceptions video if that I'll put it on the screen now but now if we did list dot get zero and save that and run it then we get something different exception good that's an exception we know what exceptions are Java checks our code if something's not right it'll give us something called an exception which will make a program stop working this exception was in the main method and it's called java.lang null pointer exception a null pointer exception and it was found at line eight right here a no point exception means that the thing you're trying to do stuff on is not there we're trying to get something at index zero on a list we know the list exists but it's not there so it's a little confusing but that's a sneak peek into the null pointer exception to fix a null pointer exception all you have to do is make sure that you're setting your object in this case an ArrayList it could be anything like a string just make sure it's set up properly in the first place that's the easiest way to avoid null pointer exceptions so to fix this we would just type new ArrayList make sure everything's all good looking save it and then maybe add something list I add like a book save it and run it we're not we're not doing anything with the book so we'll just print it out make sure everything's working all our exceptions in Java are handled and we get booked no exceptions and we're great we're great it's dandy it's dandy everything's good everything's good guys it's okay it's okay exceptions aren't scary they tell you what's going on they tell you the exact line number that things are going wrong at and they actually help you so if I helped you learn about exceptions in Java please hit that like button please subscribe if you want more videos like this and as always I appreciate you being here on this video with me it really means a lot you could be anywhere in the world but you're here with me I appreciate it and I'll catch you in the next video I'll see you later [Music]
Info
Channel: Alex Lee
Views: 99,577
Rating: 4.9060373 out of 5
Keywords: exception in java, exception java, exception java tutorial, exception in java tutorial, exception, java exceptions, exceptions in java explained, java exception, exceptions in java, exceptions java, exceptions, how to code in java, java, alex lee, how to write java code, null pointer exception java
Id: xNVlq9IEBEg
Channel Id: undefined
Length: 11min 7sec (667 seconds)
Published: Thu Apr 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.