Java Exception Handling

Java Exception Handling – FileNotFoundException

Next up in our in-depth Java Exception Handling series we’ll take a closer look at the FileNotFoundException. As you probably can guess, the FileNotFoundException is thrown when calling a number of IO methods in which you’ve passed an invalid file path. We’ll look at where FileNotFoundException sits in the Java Exception Hierarchy, and also go over some functional code samples showing how this error might come about, so let’s get to it!

The Technical Rundown

All Java errors implement the java.lang.Throwable interface, or are extended from another inherited class therein. The full exception hierarchy of this error is:

Full Code Sample

Below is the full code sample we’ll be using in this article. It can be copied and pasted if you’d like to play with the code yourself and see how everything works.

When Should You Use It?

The FileNotFoundException is thrown when attempting to access a file that doesn’t exist, so let’s just right into some code to see how it works. For this example, we’re using the Book class that we’ve used in the past to create a couple books, which we then output to a new file in the form of JSON. To easily convert from a Book object to a JSON string we are using the Jackson library, and in particular the com.fasterxml.jackson.databind.ObjectMapper() class:

The writeValueAsString(Object value) method allows us to convert the current Book instance into a JSON string value. Jackson handles all the reflection for us, with the special note that we need to implement the @JsonIgnoreProperties(ignoreUnknown = true) interface at the top of the Book class definition, which tells the parser to ignore properties it doesn’t recognize when reading from a JSON source. This is necessary because of the tagline property obtained from the Book.getTagline() method. Jackson picks up on this method and outputs the value in the produced JSON file, but since we don’t have a matching setTagline() method in the Book class, we need to ignore it during import:

Next, we have the readBookJson(String path) method, which attempts to read the JSON from the provided file path parameter, and tries to map it to an Array of Books, which are then output to the log:

Lastly, the writeBookJson(String path) method performs pretty much the opposite of readBookJson(String path), by attempting to write the data of a few Books to the provided path file location:

Note: This is obviously a bit of a hackey way to create valid JSON (explicitly writing out opening/closing brackets, and comma-separators), but the goal here is to provide a somewhat realistic example of reading/writing files, rather than a perfect illustration of working with JSON.

Anyway, with everything now setup we can test stuff out by first trying to write some data to a new file:

This creates a new books.json file and populates it with the Book data, as converted to JSON, that we expected:

Now let’s try reading the data from that file:

If everything was setup correctly, this should see the output of our Book objects, as read from the JSON. Sure enough, that’s exactly what we get:

Finally, let’s see what happens if we try to read the JSON data from an invalid file:

Lo and behold, executing readBookJson(String path) with an invalid file path throws a FileNotFoundException at us:

The Airbrake-Java library provides real-time error monitoring and automatic exception reporting for all your Java-based projects. Tight integration with Airbrake’s state of the art web dashboard ensures that Airbrake-Java gives you round-the-clock status updates on your application’s health and error rates. Airbrake-Java easily integrates with all the latest Java frameworks and platforms like Spring, Maven, log4j, Struts, Kotlin, Grails, Groovy, and many more. Plus, Airbrake-Java allows you to easily customize exception parameters and gives you full, configurable filter capabilities so you only gather the errors that matter most.

Check out all the amazing features Airbrake-Java has to offer and see for yourself why so many of the world’s best engineering teams are using Airbrake to revolutionize their exception handling practices!