Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
If you know in your application that you might generate a runtime exception, you can anticipate and handle it using a syntax called try and catch. The first thing you have to know is how exceptions behave. I'm working in a project named exceptions and it has a couple of lines of code that will generate a runtime error. I've declared a strings variable which is an array of string objects and I've initialized it with one item, a string named welcome. And then in the next line, I try to output the second item in the array, which doesn't exist.
When I run the application, I get an exception. When these kinds of exceptions happen in Java it stops your application cold, it crashes the application. I can show you that in fact this application is crashing and is not continuing with its execution. I'll add another line of code down here System.out.println and I'll output, "The application is still running1" I'll save and run the application, I'll see my error and I don't see anything after it.
So how do you handle a potential runtime error? The first thing you need to know is that when an exception occurs, a variable is generated, an exception object. The exception object will be an instance of a class named exception or of another class that's a subclass of exception. If you want to wrap your code and deal with a possible exception, the first thing to do is put the code into a try block. The try block will be followed by a catch block or more than one catch block and the code in the catch block will deal with the exception in some way.
Here is a very simple way to add a try catch block around some code that might generate a run-time error. I'm going to select that code and then I'll right click on it and I'll choose Surround With, Try/catch block. The syntax of the Try/catch block is that you start off with the word try and then you put the code that might generate the exception inside the first set of braces, then you follow that with a catch block starting with the keyword catch and then a variable declaration which is of data type exception or one of its subclasses.
Then within the catch block, you can handle the exception in some fashion. If your code execution gets into the catch block that will clear the error and your application will continue running. I'm just going to get rid of the TODO comment and save my changes and then I'll run the application again. And I still see that there is an error but then I get this extra little bit of code. The application is still running. And so I know that my try catch block has handled the exception. Within the catch block right now, I'm executing a method of the exception class called printStackTrace and that's why in the console block, you're seeing this output.
java.lang.ArrayIndexOutOfBoundsException: 1.I could take that little bit of code out, I am just going to comment it and I'll put in a little bit of my own custom code, System.out.println, there was an error. So I'm telling the user that something went wrong, but I'm being a little more vague. Stack traces tend to scare users, because they don't know what's going on inside the program and they shouldn't have to. I'll run the application again and now I get clean output.
There was an error, but the application is still running. In this situation, the particular type of exception that was thrown was not just an instance of the exception class but of one of its subclasses. You can be more specific in your catch block by knowing exactly which type of exception you are anticipating. To find out what that class is, I'm going to remove the comments and run the application again and I see that the exception object is an instance of this one, ArrayIndexOutOfBoundsException.
To be more specific about this, I'll go into the console and I'll copy the name of that exception class to the clipboard, then I'll go back to my code and I'll change the data type of the exception object. I'm declaring from exception to a rate index out of bounds exception, just by pasting that in. Then I once again comment out the printStackTrace method and run the application. And I'll see that everything is working the way I wanted it to. But now I set up a situation where as long as they get this particular exception, I'll be able to keep running the application but if any other exception happens the application will stop.
The compiler will tell me what went wrong and I'll be able to figure out what that exception was about and fix it. So that's a look at how to use the try catch structure, to anticipate errors that you know might happen in your application and then elegantly deal with them.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96774 Viewers
56 Video lessons · 110475 Viewers
71 Video lessons · 79309 Viewers
131 Video lessons · 38044 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.