Error Handling

Error handling, at the system level, is done by the D2K infrastructure. It will catch exceptions thrown by modules, and display them, as well as aborting execution and cleaning up. It is important that the system be allowed to do that, as there are tasks to be performed, and cleanup to be done, that can only be done by the system.

This is not to say that modules can not catch exceptions and do their own cleanup, but if the failure is catastrophic, then at some point the exception should be thrown so that the system can do clean up as necessary.

For example, envision a module that will read serialized data from a file. The name of the file containing the serialized object is passed in. There is no guarantee that the file actually contains a serialized object. In such a case, the exception should be caught, so local cleanup can be done, but then the exception should be thrown again:

  public void doit() throws Exception {
    String fn = (String)pullInput(0);
    ObjectInputStream out = null;
    Object object = null;
    try {
      FileInputStream file = new FileInputStream(fn);
      out = new ObjectInputStream(file);
      object = out.readObject();
      out.close();
    } catch (java.io.IOException IOE) {
      if (out != null)
        try {
          out.close();
        } catch (Exception ex) {}
      throw IOE;
    }
  }

Notice the catch block at the end. It will make sure that if the ObjectInputStream was opened, it gets closed before we exit. Since the close() method may throw an exception, it will catch the exception, but there is not much that can be done if the file cannot be closed, so that exception is ignored. The important aspect of this code snippet is that the exception will be thrown again after the output stream is closed. The exception is then caught by the D2K infrastructure, which aborts execution and performs any clean up.