Top 4 Java exceptions Raygun can help fix

| 4 min. (663 words)

There are a bunch of Java exceptions that are common to all code bases, and frequently occur at both compile time when implementing, and runtime when your application is in the wild. This last case is crucial to have visibility around – most developers may use a logging framework, or perhaps a hand-rolled notification system. The advantage of using a real error tracking service is huge. Here are some of the most common Java exceptions, and how Raygun can capture these at runtime and help you find the source of the bug, leading to reliable software and happy end users.

NullPointerException

Tony Hoare’s billion-dollar mistake of including ‘null’ in a modification to the Algol language must still be taken into account when dealing with the type system in Java (you will of course note that it’s a legit failure state, which can be handled in safer ways in languages like Rust). The act of accessing an uninitialized variable, or one that has been deliberately set to null, blows up strongly-typed code at runtime as the type system can’t provide any guarantees for the object (by design). Knowing when your code attempts to use null variables is crucial. If you’re in a web context, frequently a third-party API may return some error state in response to a request, and the variables which would have otherwise been set would be null.

(I know – it’s Knuth, not Hoare, but close enough. The hat sells it.)

Seriously though, Raygun will capture every instance of your code attempting to use null objects, and report the stack trace so you can pinpoint the issue. The Raygun4Java provider also allows you to set custom data, so you can provide the state of relevant variables, allowing you to easily diagnose NullPointerExceptions on your production hardware from your local machine.

IOException

This exception type (and others in its family) can be thrown when invalid file access occurs, which is most likely to occur when implementing a local application when debugging is easier. Networking errors can also result in this exception being thrown, which is more critical when this happens in production web applications or services.

It is particularly interesting how often connections can fall down at the network layer – for instance, database query timeouts. These can be quite frequent, especially if your code is running in a public cloud with noisy neighbours, or in general if resource contention is a factor. Raygun will capture instances of these exceptions, giving you insights as to how often these are occurring over time, which would be very difficult to see from view log files.

Concurrent (threading) exceptions

Exceptions like InterruptedException are raised when your thread receives an interrupt while it is executing. You must catch this or return it to be handled higher up (depending on whether it makes sense for the current method to do so). These and similar concurrent exceptions may only start to appear under heavy load in production when contention for threading resources becomes a factor. It is important to use a tool like Raygun that can track and group these sensibly, so you can see how often and when these are occurring as their transient nature can be quite difficult to reproduce locally.

IllegalArgumentException

This is one you should be throwing in your own code, so that at runtime when a method receives invalid input you can throw an appropriately typed exception. Raygun can show instances of these, and as in the null pointer case, you can attach custom data to see the particular invalid arguments that were passed in (for instance, zero as the denominator for a Divide() method). This would be invaluable when developing anything that accepts user input, for instance a web API or service, mobile app, or desktop application.

Get started with Raygun now

Interested in trying professional-grade error tracking? All of these insights and more are available with Raygun, so start your free 14-day trial now, no credit card needed. Happy exception blasting!