Java Driver 1.0 for Apache Cassandra

Exceptions

Handling exceptions.

Overview

Many of the exceptions for the Java driver are runtime exceptions, meaning that you do not have to wrap your driver-specific code in try/catch blocks or declared thrown exceptions as part of your method signatures.

Example

The code in this listing illustrates catching the various exceptions thrown by a call to execute on a session object.

   public void querySchema() {
      Statement statement = new SimpleStatement(
      		"INSERT INTO simplex.songs " +
      				"(id, title, album, artist) " +
      				"VALUES (da7c6910-a6a4-11e2-96a9-4db56cdc5fe7," +
      					"'Golden Brown', 'La Folie', 'The Stranglers'" +
      		");");
      try {
         getSession().execute(statement);
      } catch (NoHostAvailableException e) {
         System.out.printf("No host in the %s cluster can be contacted to execute the query.\n", 
         		getSession().getCluster());
      } catch (QueryExecutionException e) {
         System.out.println("An exception was thrown by Cassandra because it cannot " +
         		"successfully execute the query with the specified consistency level.");
      } catch (QueryValidationException e) {
         System.out.printf("The query %s \nis not valid, for example, incorrect syntax.\n",
         		statement.getQueryString());
      } catch (IllegalStateException e) {
         System.out.println("The BoundStatement is not ready.");
      }
   }