Java Exception Handling

Java Exception Handling – ConnectException

Moving along through our detailed Java Exception Handling series, today we’ll dive into the ConnectException in Java. As you can probably guess, the ConnectException indicates an error has occurred while attempting to connect a socket to a remote address, such as when trying to access a URL that doesn’t exist, or which refuses outside connections.

In this article we’ll examine the ConnectException in more detail, looking at where it sits in the Java Exception Hierarchy. We’ll also go over a bit of functional sample code that aims to illustrate how ConnectExceptions are typically thrown using built-in libraries, and also how you might throw your own instances when necessary, 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?

Since the ConnectException is only used when dealing with remote connections, we’ll jump right into our code, which attempts to create a basic connection through the HttpURLConnection class (technically we’re using the HttpsURLConnection class, since this is a secure connection, but this class inherits from HttpURLConnection, so that’s what parameter type our method signatures declare).

We start with the connect(String uri) method, which creates a new URI instance and then tries to establish a connection via the openConnection() method:

As you can see, we perform processing on that connection object in the processResponse(HttpURLConnection connection) method:

Right now this is just an intermediary method, since it merely passes the connection parameter along to the final method in the chain. However, we include this to show where we might perform our logic based on the connection object properties and responses that were provided. Finally, we call the logConnection(HttpURLConnection connection) method, which nicely formats a bit of basic information about the connection and outputs it to the log:

With everything in place let’s try connecting to a few different addresses and see what happens:

We start with https://www.airbrake.io, which should work just fine. Sure enough, the log output of the first call shows a connection was established and an OK / 200 response code was returned:

However, when we try our second call using the URI for brakeair.io — a (currently) non-existent address — our connection attempt throws a ConnectException at us:

Quick and easy. That’s the usage of the ConnectException in a nutshell. However, it’s briefly worth noting that the actual error message content that we see above isn’t all that revealing. Therefore, in some cases, it might be worthwhile to throw a ConnectException yourself with a more detailed error message. For example, here we’ve modified the processResponse(HttpURLConnection connection) method a bit:

As you can see, we explicitly check if a ConnectException is caught that has an error message of "Connection refused: connect". If such an exception is caught we throw a new ConnectException with a more detailed message.

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!