Try / Catch & Exceptions | Java | Tutorial 25

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Academy my name is Mike in this tutorial I want to talk to you guys about catching exceptions in Java more specifically we're going to talk about using something called the try catch blocks and basically what this allows us to do is it allows us to be able to monitor for specific errors that might occur in our program and if those errors occur then we can actually just handle them so if a pro if an error happens that would normally cause our program to stop executing we can handle that error and keep executing the program so I'm going to show you guys a simple example of how to do this and you can kind of apply this to a variety of situations so down here I have a scanner setup basically all I'm doing is I'm using this scanner to get a number from the user so I'm basically saying hey print out a number basically getting the next double that they input so I'm saying keyboard input next double this will accept a number as an input and I'm storing that number inside of this num double variable then I'm just printing it out so it's a super simple program I'm just gonna run it and over here it says enter a number so I can enter in a double with like three point nine oh eight and it just prints it out onto the screen awesome so we have a nice little program here but here's the problem if I was to run this program again and instead of putting a number in here I was to put like a letter so god forbid I put something other than a number when I click enter we're gonna get this exception so you can see down here the program actually stopped running so this caused the programs who completely terminate we didn't print out the number we didn't store it inside the variable the program just ended right our program blew up it's done and it says over here exception in thread main Java util dot input mismatch exception basically what this means is Java threw an exception which means something happened that Java didn't like that Java couldn't handle and the program had to terminate and in our case it was an input mismatch exception which basically means they entered in a string when they should have entered in a double so they entered in a double everyone's good everyone toppy but because they entered in a string uh uh Java can't handle that and it can't handle it because Java was supposed to store this inside of a double but it can't store a string inside of a double so the program broke and this is a problem this is a real problem in our programs if you're writing a program and you know somebody enters in a string when they're supposed to enter in a double you don't want your program to blow up right you don't want it to just completely stop working what you want to do is be able to recognize like hey this person entered in the wrong type of input let's tell them that and then we can go off and do something else and I'm going to show you guys how we can do something like that so how we could catch one of these exceptions we're gonna have to use something called the tried catch blocks and basically the way we set this up is I just say try I make an open and closed curly bracket and then down here I just want to say catch and catch is going to take a parameter so we're gonna give it some open and closed parentheses inside of these parentheses I just want to say exception and I'm just gonna call this e so basically an exception is getting passed into this open and closed parentheses if that doesn't make total sense what we're doing here just stick with me and it'll make sense in a second so down here I'm saying it's system that outs out print line and I'm getting the number what I can do is I can actually put this stuff or actually I just want to put these two lines down here inside of this try block and I'm always gonna ask them to get the number so we'll put that up here inside of this try block inside of these open and closed curly brackets I'm actually asking for the user's input and then I'm printing it out and what this is gonna do is it's basically gonna allow us to do this and if our program blows up when we're doing this in other words if an exception gets called when we're doing this then we'll execute the code down here in this catch block so I'm actually gonna copy this and instead of printing out the number we're just gonna print out invalid input okay so now that we have these try-catch blocks set up I'm gonna show you what we can do with them so if I was to come back over to my program and recreate the situation that happened before so I'm just gonna enter in a letter instead of entering in a double now instead of our program terminating and just completely blowing up and we're done now it just says invalid input so it's able to handle what just happened right it's able to handle this situation so that's the beauty of try-catch blocks is they let you look for errors and catch them and then when your program throws an exception we can just print something else out so we can give the user a message like hey you put in the wrong stuff how dare you we can also get more specific with these and I actually want to talk to you guys about this exception e overhear exception is what's called a class in Java and for the most part it's just like a special type of data type so we have like strings we have integers we have boolean's and exceptions very similar to that it's just it's storing some information inside of it and the exception is basically going to tell us what went wrong so if I was to come down here inside of this print line statement I could just print out E now E is the name of the exception that got passed in if I print out E it's gonna tell me what went wrong so if I typed in a over here again instead of printing out invalid input it's gonna tell me what exception was thrown so it's gonna say Java dot util dot input mismatch exception got thrown so this is actually a pretty interesting message right it's telling me what went wrong it's telling me what type of exception was thrown it was an input mismatch exception so let me show you guys another example of using something like this so down here I actually have some code that I commented out and I basically just created a an array so I created an array and I gave it three elements so I just gave it three numbers so it's just 1 2 & 3 and then what I did was I tried to print out the fifth element inside of this array now this array only has three elements right and they're an index positions 0 1 & 2 if I try to print out the 5th element in this array or the element at index position five my program is gonna blow up and I'll show you guys what's gonna happen so before I even get to enter in the the input actually let me put this down here I'm gonna roll my program and it's immediately gonna blow up and we're gonna get another exception you can see over here it says exception and thread main Java dot Lang dot array index out of bounds exception so before we were getting an input mismatch exception now we're getting this array index out of bounds exception this is a different type of exception that we can use in our program and Java basically told us like hey there's no array position at this index so Java couldn't handle that and the program ended up terminating it ended up throwing an exception so there's different types of exceptions for different situations that you're gonna get into in Java and actually what we can do is we can catch different types of exceptions so for example if I was to take this line of code right here that's printing out nums five right it's printing out that fifth index and this is the piece of code that breaks if I was to put this down here inside of this try block so I'm putting it down here this is also going to get caught by this exception so we're also just gonna print out e so now when I run my program instead of breaking it's just gonna say Java dot Lang dot index out of bounds exception five so the program didn't explode it didn't terminate we didn't throw an exception it just printed out what the exception was so if I come down here and I just use this exception datatype if I just say exception it'll catch any exception that's put inside of these try blocks but if I want instead of just putting this exception I can also add in specific types of exceptions right so for example down here we have two pieces of code that are gonna break this program right we have this system that opt out print line that's trying to print out an invalid array index we also have this line of code right here if the user enters in something that's not a double that's also gonna cause an exception but remember these are causing two different types of exceptions the first one is causing an array index out of bounds exception this one is causing a input mismatch exception they're two different things that are going wrong there's two completely different reasons why a Java program blew up right when I just use this general exception word down here this will catch any exception under the Sun it'll catch any possible you know situation where Java threw an exception this is going to catch it but what happens if I have two pieces of code that are throwing different exceptions and depending on the exception that gets thrown I want to do different things well I can actually specify what type of exception I want to catch so for example this is just catching like any old exception under the Sun but what if I wanted to specifically catch this array index out of bounds exception well what I can do is I can actually include a catch block that will catch this specific error so I can copy this this big long name over here and instead of printing out exception over here I'm just gonna put array index out of bounds exception and this is going to specifically catch this situation right here where the number explodes so now if I run my program the same thing is going to happen right so it's just gonna do exactly what it did before it'll be able to catch that array index out of bounds exception but if I got rid of this line so I'm just gonna comment this out so this is no longer gonna be inside of my program and I mess up the keyboard input so I'm gonna come over here and we'll mess up this keyboard input so I'll put another letter this isn't this isn't catching this exception anymore because this catch block only catches array index out of bounds exception z' that's all it can do right it can't catch the input mismatch exception all I can do is catch the index array out of bounds exception so you can actually include multiple catch blocks in here so for example I could say like another catch block and now we can include that other errors so for example our error over here was input mismatch exception so I'm gonna copy this guy and I'll throw this into this second catch block and again we'll just call it Eve so now this is gonna be able to catch this input mismatch exception and you'll see I'm getting an error here so sometimes for some of these exception types you're gonna have to import them that basically means you have to tell Java that you want to use them and that's only for some of them and if I'm on eclipse I can just come down here and click import and you'll see up here at the top of the file it imported Java util you input mismatch exception so now I can put some code over here so I can just system to print line and we'll say invalid and what this will do is it'll be able to catch that invalid input so now when I run this program I can enter in a letter so I can enter in the wrong thing here and it just says invalid input so if you want to catch specific types of exceptions then you can specify those specific types of exceptions inside of these catch blocks or you can do what we did before which was just use instead of one of these you can just say like exception and this will catch any possible exception that you can have so a lot of times what people will do is they'll catch you know maybe like two or three specific types of exceptions and then over here they'll just use that general exception and this will just catch anything else so you know you can have all these so first it'll check if it's an array index out of bounds then it'll check if it's a mismatch input and then if it's neither of those it'll just throw a general exception error so basically the use of exceptions is just so your program doesn't blow up right if you're writing a you know a Java program that's gonna be running for you know months and months at a time you don't want it to blow up no matter what so you always want to make sure that it's able to handle whatever gets thrown at it and you can use these try catch blocks to do that so in addition to exceptions there's also another type of problem that can go wrong in our which is called an error and the main difference between an exception and an error is that an error is like really serious so if you have an exception in your program for example like when the user inputs a letter instead of a number it's not serious right nothing is like actually going wrong in the program it's just you know something happened that Java couldn't necessarily handle but in addition to an exception there's also something called an error and an error is generally something that you don't want to catch because an error means that something really went wrong like something is like seriously wrong with the program and there's no you know possible way that we can recover now that doesn't mean that you can't catch an error in your programs but Java would say that any reasonable application shouldn't be trying to catch an error so the errors are just abnormal conditions you know things that you really don't want to happen in your program no matter what but I'm gonna show you guys basically how to handle them anyway I just wanted to talk to them to talk about them so you're aware of them so instead of saying exception down here I could say error and this would basically allow me to catch any errors that get thrown now you can also call something called throwable so I could say throwable and basically throwable is going to catch either an error or an exception so if you want to catch any errors and any exceptions that occur inside your program if you say throwable that'll basically just catch anything so remember when we just said exception there that caught any exception that could be thrown if we would say error here this will catch any error that gets thrown and there's also some specific errors just like we had specific exceptions but if you say throwable this will catch like any error or any exception so basically like anything that goes wrong with the program this will catch it but again Java recommends like I'm the Java Docs it recommends that you don't any reasonable application doesn't try to catch errors but you should definitely try to catch exceptions so that's the basics of exceptions and we talked a little bit about errors we talked a little bit about throwable there's a lot more to kind of discover with these things obviously you can learn about all the different you know specific exceptions but this kind of gives you an idea of how to implement them in your programs hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop Academy to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying traffic Academy and you want to help us grow head over to draft Khadem EECOM forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 28,122
Rating: 4.9829059 out of 5
Keywords: Programming
Id: JTjeGpSUL2M
Channel Id: undefined
Length: 15min 56sec (956 seconds)
Published: Sat Oct 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.