Java Exception Handling

Java Exception Handling – NoSuchMethodError

Making our way through our comprehensive Java Exception Handling article series, today we’ll be looking into the NoSuchMethodError. As you may suspect based solely on the name, the NoSuchMethodError is thrown when a class can no longer locate the method definition of the specified method. While this error is typically caught by the compiler, it’s possible to find yourself in scenarios where it can occur during runtime, such as when a class was modified without other dependant classes being updated.

In this article we’ll explore the NoSuchMethodError in more detail by first looking at where it sits in the larger Java Exception Hierarchy. We’ll then go over some functional sample code that illustrates how NoSuchMethodErrors might actually occur during runtime, depending on your own particular development habits and practices. With that, let’s get this party started!

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?

As mentioned, in most cases a potential NoSuchMethodError will be caught by the compiler or integrated development environment (IDE) you’re working with. For example, the Jet Brains IntelliJ IDEA IDE explicitly highlights and provides an internal error in the editor when a code statement references a class method that no longer exists. Thus, in most scenarios, it’s fairly unlikely to see a NoSuchMethodError during actual runtime. However, that doesn’t mean it’s not possible, particularly as projects get larger or multiple people become involved. All it takes to see a runtime NoSuchMethodError is to have one class modified and recompiled, while a dependant class does not get recompiled at the same time. In those situations, the outdated class may still reference a class method that no longer exists, causing a runtime NoSuchMethodError.

To see this in action with functional code we start with our basic Book class:

Nothing special going on here, we’re just using this to represent a possible scenario of the “causal” class that might be updated and recompiled. Our Main class then has the createBook() method that does just as it says, creating a Book instance:

Notice that we’re explicitly calling the book.toFormattedString() method, which outputs the Book into a formatted string as seen below:

It’s worth noting that we normally would override the built-in toString() method of the Book class for this purpose, but since Java automatically generates that method if it’s not explicitly defined in a class definition, it wouldn’t work for our purposes here.

Anyway, executing createBook() works fine and produces the output that we expect:

Now comes the trouble. Imagine we’ve made some changed to the Book class. Specifically, we’ve removed (or renamed) the toFormattedString() method. For our purposes here, we’ll just rename it to toString(), since that is the “traditional” way to handle this:

Now, let’s recompile just the modified Book class:

Now, our Main class hasn’t changed, and it also includes a second method for creating a poem instance of Book this time:

Executing the createPoem() method now throws a NoSuchMethodError:

As we can see, the problem here is that the call to poem.toFormattedString() references a method that no longer exists, since we renamed it to toString() and recompiled the Book class.

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!