Viewers: in countries Watching now:
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
As I showed in a previous video, a runtime exception generates an instance of a class called an exception class, either the exception class itself or one of its subclasses. I've shown how to deal with this with a try catch block around the possibly offending code. But I'd also like to show you how to explicitly declare some code that might throw an exception and wrap it in a method that has a keyword called throws, that tells the rest of the application what might happen. Here is the same starting code as in a previous video, instead of wrapping this directly inside try catch, I'm going to take this code and put it into its own method.
I'll select these two lines of code right click on the selected code and choose Refactor, Extract Method. I'll give the method a name of getArrayItem, and right here I'm going to select an option of Declare thrown runtime exceptions. This sort of refactoring, taking code that might generate a runtime exception and putting it into its own separate method is such a common practice that Eclipse has a special option for it. I'll click OK and that creates the new method.
Now notice, it didn't add any additional code and the reason is because in this situation none of the code that I executed already told Eclipse what kind of runtime exception might happen. So I'm going to put in the code myself, I'll place the cursor after the name of the method, now at this point, the code is going to get a little bit wide. So I'm going to go to a new line here and I'll put in the keyword throws and then add Array press control space and choose the exception class ArrayIndexOutOfBoundsException.
This now tells the rest of the application that when this particular code is running, that is the code within the getArrayItem method, that it could throw this specific kind of exception object. Now, I'll go to the method getArrayItem, I'll select that, I'll right click and I'll select Surround With, Try/catch Block and now Eclipse is smart enough to know that because I'm calling a method that could throw this object, that's the one it's looking for. I'll get rid of the TODO comment, I'll replace e.printStraceTrace with my own custom message, System.out.println " Array item was out of bounds" and I'll run the application.
And now I'm elegantly catching the error, handling it myself, and the rest of the application can keep on running. So again, the common practice is when you have a bit of code that might generate an exception, put it in its own special method and mark the method with the keyword throws and the particular kind of exception class that might be thrown.
Find answers to the most frequently asked questions about Java Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
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.