Catching Exceptions from the Java Client Library


Just like our other client libraries, if you're using the Java client library and encounter a server error, an exception will be generated. This will cause Java to stop in its tracks and print out the contents of the exception. While this is useful for debugging, in a production application you'll probably want your application to fail gracefully, perhaps displaying a nice error message to your users.

You can do this by wrapping your code in a try block and using an catch statement:

try {
  // Do something that accesses the network
} catch (IOError e) {
    // Insert custom error handling code here.
    System.out.println(e.toString());
} catch (ServiceException e) {
    // Insert custom error handling code here.
    System.out.println(e.toString());
}

Most errors will be instances of either IOError or ServiceException, depending on the cause.