From the course: Java 8+ Essential Training: Objects and APIs

Handle exceptions with try-catch - Java Tutorial

From the course: Java 8+ Essential Training: Objects and APIs

Start my 1-month free trial

Handle exceptions with try-catch

- [Instructor] Whenever you write code that could potentially throw an exception at runtime, you can wrap that code or surround it in a try catch block. I have code here, and I'm going to introduce an exception. As I did in a previous video, I'm going to use the wrong index value by using chars.length instead of chars.length minus one. And when I run the application, I get an array index out-of-bounds exception. Now I'm going to select all this code, and then I'll go to the menu and choose Code, Surround Width. I see this window, Surround Width, and I can choose from all these different clauses that I can wrap around the code. I'll choose Try Catch, option number six. Here's the basics of how try catch works. The code you want to run goes inside the try block, between the braces. As long as no exceptions are encountered, the code will execute one line at a time. If all that code runs successfully, then the flow will jump to after the try block, and it'll skip the catch section entirely. So if I set the code like this, with chars.length minus one, and then I run the application, I'll see code ran successfully. If I take away the minus one and run the application again, then I won't run to completion, and I'll crash with the array index out of bounds exception. Notice that I still get the message code ran successfully, but I get the output from the catch block. This display of the array index out of bounds exception class is caused by this code, e.printStackTrace. If I don't want to display the message code ran successfully, I can put a return statement right here. And now I'll see the StackTrace showing me the exception, but I won't see the final statement. This is frequently called structured exception handling, and it gives you a way of anticipating exceptions that might happen in your code. Now, the catch block right now is looking for something called the exception class. The exception class in Java is the highest level exception you can have. It's a super class of other classes. Now we haven't talked about inheritance and polymorphism and all those more advanced subjects yet, but it's essential to understand the basic nature of exceptions, if you're going to code this way. A catch clause that's looking for an instance of the exception class will be called regardless of what kind of exception is caused. So it can be caused by an array index out of bounds exception, a null pointer exception, or any other exception. Let's say I wanted to very specifically look for array index out of bounds exception. So I'll change the nature of the exception I'm looking for to array index out of bounds exception, like that. And then, I'll introduce another kind of exception. As I did in a previous video, I'll create a string variable named nothing, which is set to null, and then I'll output nothing.length. I'll run the application, and right now, I'm getting my array index out of bounds exception. Change this express once again to chars.length minus one. That eliminates that exception, but it doesn't eliminate the other exception, the null pointer exception. I see a successful output of the last character, but then once again, the code crashes. So not all exceptions are alike. But Java gives you plenty of ways of handling this, and I'll show you a couple of strategies in the next video.

Contents