So here’s the thing.  I’m not REALLY a Java guy. Yes I did, however, write most of the starting code for WooMinecraft, but that was more of a test on getting into Java itself, not really to write the plugin.

My day job is as a WordPress developer over at WebDevStudios. So I’m really limited on the time I get to spend with Java, which makes it more of a hobby and a side project. This is evident if you look at the commit logs for WooMinecraft.

However, this weekend I’ve started work on a sort of “remote white-list management” for Minecraft. To get you up to speed, Minecraft, while being a game, is still pretty in-depth if you play modded Minecraft like Feed the Beast or Tekkit, or other packs.  Anyhow, I’m not going to try to explain it completely, but basically for me, Minecraft is a Java playground.

So What’s the Problem?

Earlier today I was writing some java code based on a StackOverflow question, which basically was “How do I do an HTTP Request in Java”, simple right? Well in WordPress we have something nifty called wp_remote_get() or _post() respectively.

With WordPress it can look something like so:

<?php
/**
 * Gets the homepage HTML string of google.com
 *
 * @author JayWood
 * @return string
 */
function get_google_webpage() {
    $http_request = wp_remote_get( "http://google.com" );
    if ( ! is_wp_error( $http_request ) ) {
        return wp_remote_retrieve_body( $http_request );
    }
    return '';
}

Pretty simple in WordPress, we don’t have to deal with input streams, connection parameters ( unless we want to ). Granted, I should note that wp_remote_get is a wrapper for a few things, but still, it makes it super simple…

How Java Differs

Well, if it’s not obvious, Java is NOT WordPress. But aside from that, the code I found on StackOverflow today was well, still quite simple:

    public static String getHTML(String urlToRead) throws Exception {
      StringBuilder result = new StringBuilder();
      URL url = new URL(urlToRead);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line;
      while ((line = rd.readLine()) != null) {
         result.append(line);
      }
      rd.close();
      return result.toString();
   }

However, in the interest of ‘handle all the things’ I figured, I could do it BETTER!!! Remember, I’m still a java noob, so better isn’t always better. Mainly I thought it best to catch ALL exceptions, so I made this:

    /**
     * Gets page content from a URL.
     *
     * @param String urlToRead
     * @return String
     */
    public String getPageContent( String urlToRead ) {
        StringBuilder result = new StringBuilder();

        URL url = null;
        HttpURLConnection connection = null;
        try {
            url = new URL( urlToRead );
        } catch ( MalformedURLException e ) {
            getLogger().severe( e.getMessage() );
        }

        if ( null == url ) {
            return "";
        }

        try {
            connection = (HttpURLConnection) url.openConnection();
        } catch ( IOException e ) {
            getLogger().severe( e.getMessage() );
        }

        if ( null == connection ) {
            return "";
        }

        try {
            connection.setRequestMethod( "GET" );
        } catch ( ProtocolException e ) {
            getLogger().severe( e.getMessage() );
        }

        InputStream inputStream = null;

        try {
            inputStream = connection.getInputStream();
        } catch ( IOException e ) {
            getLogger().severe( e.getMessage() );
        }

        if ( null == inputStream ) {
            return "";
        }

        BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream ) );
        String line;
        try {
            while ( null != ( line = reader.readLine() ) ) {
                result.append( line );
            }
            reader.close();
        } catch ( IOException e ) {
            getLogger().severe( e.getMessage() );
        }

        return result.toString();
    }

But this got me thinking, Exception is pretty much the base class of exceptions in Java ( don’t quote me on that ). So is this too much?

Can Java Catch Multiple Exceptions?

This is the main question around this blog post. In the above code you’ll see that I’m catching the following exceptions:

  • MalformedURLException
  • HttpURLConnection
  • ProtocolException
  • IOException

So, let’s break it down — MalformedURLException extends IOException, as does ProtocolException. IOException extends Exception. Which reaffirms the notion that Exception is the base class for all Exceptions within the StackOverflow code.

Does this mean the code from StackOverflow will work without error and properly catch the exceptions necessary, despite the fact that multiple exception types could be thrown?

Perhaps, as I do, I’m over-engineering the solution in the interest of ‘log all the things’.

So I guess the question is this – does creating a helper method ( like in the StackOverflow code ) which throws Exception, in-turn, catch any/all exceptions which extend the Exception class?