The Top 10 Exception Types in Production Java Applications — Based on 1b Events
Here is a rundown of the top 10 most commonly encountered exceptions, a massive majority of which boil down to a few different sources.
Join the DZone community and get the full member experience.Join For Free
the pareto logging principle: 97% of logged error statements are caused by 3% of unique errors
cwe received a lot of feedback and questions following the latest data crunching post where we showed that 97% of logged errors are caused by 10 unique errors . by popular demand, we’ll go a step deeper into the top exceptions types in over a 1,000 applications that were included in this research.
without further ado: the top exceptions by types
to pull out the data, we crunched anonymized stats from over a 1,000 applications monitored by takipi’s error analysis micro-agent, and checked what were the top 10 exception types for each company. then we combined all the data and came up with the overall top 10 list.
every production environment is different, r&d teams use different 3rd party libraries, and also have custom exception types of their own. looking at the bigger picture, the standard exceptions stand out and some interesting patterns become visible.
1. nullpointerexception — 70% of production environments
yes. the infamous nullpointerexception is in at no. 1. sir charles antony richard hoare, inventor of the null reference was not mistaken when he said:
“i call it my billion-dollar mistake. it was the invention of the null reference in 1965… this has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years”.
with a top 10 spot at 70% of the production environments that we looked at, npes take the first place. at takipi , we actually have a special alert that lets us know whenever a new nullpointerexception is introduced on our system, this is how you can set it up yourself .
2. numberformatexception — 55% of production environments
in at no. 2 is the numberformatexception which happens when you try to convert a string to a numeric value and the string is not formatted correctly. it extends illegalargumentexception which also makes an appearance here at no. 3.
one easy fix to make sure that the input you’re passing to the parse method passes these regular expressions:
- for integer values: “-?\\d+”
- for float values: “-?\\d+.\\d+”
3. illegalargumentexception — 50% of production environments
next up at no. 3, illegalargumentexception, appearing at the top 10 exceptions in 50% of the production environments in this survey.
an illegalargumentexception actually saves you from trouble, and thrown when you’re passing arguments from an unexpected type to your methods. for example, some method that expects type x and you’re calling it with type y as an argument. once again, an error that’s caused by not checking what you’re sending out as input to other methods.
4. runtimeexception — 23% of production environments
all exception objects in the top 10 list (apart from exception) are unchecked and extend runtimeexception. however, at no. 4 we’re facing a “pure” runtimeexception, where java the language actually doesn’t throw any itself. so what’s going on here?
there are two main use cases to explicitly throw a runtimeexception from your code:
- throwing a new “generic” unchecked exception
- “wrapping” a general unchecked exception around another exception that extends runtimeexception
- making a checked exception unchecked
one famous story around checked vs. unchecked and the last use case we described here comes from amazon’s aws sdk which only throws unchecked exceptions and refuses to use checked exceptions.
5. illegalstateexception — 22% of production environments
in at no. 5, featured at the top 10 exceptions in 22% of over a 1,000 applications covered in this post is the illegalstateexception.
an illegalstateexception is thrown when you’re trying to use a method in an inappropriate time, like … this scene with ted and robin in the first episode of how i met your mother .
a more realistic java example would be if you use urlconnection, trying to do something assuming you’re not connected, and getting “illegalstateexception: already connected”.
6. nosuchmethodexception — 16% of production environments
such method, much confusion . about 16% of the production environments in this data crunch had nosuchmethodexception in their top 10.
since most of us don’t write code while drunk, at least during the day, this doesn’t necessarily mean that we’re that delirious to think we’re seeing something that’s not there. that way the compiler would have caught that much earlier in the process.
this exception is thrown when you’re trying to use a method that doesn’t exist, which happens when you’re using reflection and getting the method name from some variable, or when you’re building against a version of a class and using a different one at production (thanks @ ).
7. classcastexception — 15% of production environments
a classcastexception occurs when we’re trying to cast a class to another class of which it is not an instance. according to our data, 15% of production environments have it in their top 10 exceptions, quite troublesome.
the rule is that you can’t cast an object to a different class which it doesn’t inherit from. nature did it once, when no one was looking, and that’s how we got the … java mouse-deer . yep, that’s a real creature.
8. exception — 15% of production environments
in at no. 8 is the mother of all exceptions, exception, dun dun duuuun (grandmother is throwable).
java never throws plain exceptions, so this is another case like runtimeexception where it must be … you, or third party code, that throws it explicitly because:
- you need an exception and are just too lazy to specify what it actually is.
- or … more specifically, you need a checked exception to be thrown for some reason.
9. parseexception — 13% of production environments
parsing errors strike again! whenever we’re passing a string to parse into something else, and it’s not formatted the way it’s supposed to, we’re hit by a parseexception. bummer.
it’s more common than you might have thought with 13% of the production environments tested in this posted featuring this exception in their top 10.
the solution is … yet again, check yo’ self.
10. invocationtargetexception — 13% of production environments
another exception that’s thrown at us from the world of java reflection is the invocationtargetexception. this one is actually a wrapper. if something goes wrong in an invoked method, that exception is then wrapped with an invocationtargetexception.
to get the original exception, you’d have to use the gettargetexception method.
we see that 13% of production environments tested in this post had it in their list of top 10 exceptions. this is the second exception type here that’s directly related to java’s reflection features.
the world of java exceptions is indeed quite colorful, and it’s amazing to see how much impact the top 10 exceptions have on our logs. 97% of all logged errors come from 10 unique exceptions.
try takipi and find out what are the top 10 exceptions in your own production environment, it only takes a few minutes to get started and you’ll also get all the data you need in order to fix them. source, stack, state.
Published at DZone with permission of Alex Zhitnitsky, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.