Simple Example

5 minutes
Share the link to this page
Copied
  Completed
A try statement is used to catch exceptions that might be thrown as your program executes. You should use a try statement whenever you use a statement that might throw an exception. That way your program won’t crash if the exception occurs.

Transcript

Hello, in this programming video, I am going to cover exceptions or just a simple example. And over the next few tutorials, we'll go more in depth into exceptions, you may know it as detroy catch block as well, that's usually what I refer to me, not because they're not trying catch as part of it. Okay, so the format you'd like to try you properly braces, cat, open bracket, you put the name of the exception, which will add one in a second. And then the curly braces like so. And the whole point is something that might, you know, cause an error during the running of the application, I can make the application crash. You can use a try catch.

And if you you know, handle or, you know, try and catch for that exception, that error if he occurs, you'll get your Here, you can handle it, but you will not actually crash the application. So if, for example, maybe you load in a file, we're not going to use that grab probably something a little simpler, actually go back to the file IO part of this series, and enclose what we did in the in the try catch block, the way we will cover momentarily in this video. So if we try and load a file, that file doesn't exist. And it you know, say crushes the application. Maybe we have a for the loads in settings for game. And if the following this decode has default settings, so the try catch block could be used for that.

So what I'm going to do is create a simple example. So I'm going to create an array called array and a new insistently of size and, and in here, what I want to do is the system are going to learn. And I'm going to print out the array at index 10. Remember, it's always n minus one. So it starts from zero to n minus one to zero. So the indexes are 012345678 and nine, for this case, this is, you know, it bands.

And the exception that we need to use for this example is called array index. And bounds exception and I'm gonna call it error. I will provide a link with the video so you can have a look at all the different exception and I'll I can show you in a second an easy way of pointing right which exception you need while developing the system. The printer land and I'm just going to print out here. So if we run this, notice it Haven't crashed. But it has given us you know, it's saying the problem is it's caught this exception is because we're trying to access the index number 10.

Prove it doesn't crash. If I do system, dot out dot print, ln. And as you can see, it still hits it. So let me show you what happened. This must be 20. Same problem, you just say the problems are 20.

Before so knowing, for example, which is the last index is fine, it prints out zero because that's what you know, automatically assigned, they know how to do B to do an integer. That's fine to remember what data type it is. Let me show you how you can sort of identify the exception that you might need and what it looks like. If you Don't actually handle this. So if I literally get rid of this code here, so all I'm going to do is go straight to the printed out. As you can see, fine.

We go on. And for example, not only was this last line not printed because he actually crashed here, it printed out the error, this, you know, actually crashes and sends an error. So it's actually said, exception in thread main and is provided exception right there for while you're developing your application. If you can think of how to, you know, replicate the error replicated, and he'll tell you in the console, what the exception is, I can just use that exception right here with this. What happens is it goes to this catch block, and you can handle it accordingly. So if you're loading a file, maybe have some default values.

If you're trying to access an array, maybe you have asking you For some information, you can, you know, ask them again, for example, it doesn't crash the application. That's not what you want in the end application. That's not a very good user experience. So that's it for exceptions. We'll cover more in depth example of the next few videos. If you have any questions, feel free to reach out, and I look forward to seeing you in the next awesome Java video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.