Handle Java errors

- [Voiceover] So let's look at handling errors in Java.…So here I have some code that loads a file…and closes out the FileReader.…It throws an IOException.…So we have two main ways of handling errors.…We can have the method that we're running the code in…that could possibly create an error, throw an exception…or we can handle it using a try-catch block.…So let's see what happens when our hello file is not there…because I've actually deleted the file so it's no longer…in the Package Explorer.…

I run the application and I'm seeing this error here.…So it says there's an exception in thread main, hello…no such file or directory.…So let's see what happens differently if we handle it…in a try-catch block.…So I'm going to delete the throws IOException…and I'm going to handle the error myself.…So I'm going to try and wrap all of this code…in curly braces and I'll indent it so it's easier to read…and then in here we can catch the error.…

So we'll catch IOException, we'll call this error…and then we'll just print out the error that we get.…

Resume Transcript Auto-Scroll

Author

Updated

8/1/2016

Released

2/22/2016

Get an introduction to Java programming while you learn to develop Java applications for various runtime environments—desktop, web, and mobile—with Eclipse. Author Todd Perkins explains how to configure the development environment, and reviews the basic building blocks of the Java syntax: variables, functions, arrays, conditional statements, classes, and input/output. In chapters 3 and 4, he builds a user interface with the Swing framework, the GUI toolkit for Java, and connects Swing's table components to different data sources. He also covers NetBeans, an alternative IDE for working with JavaServer Pages (JSP), and publishing workflows for desktop applications (via JAR files) and Android apps built with Java.