sk.uniba.euromath.tools
Class ExceptionRegistry

java.lang.Object
  extended by sk.uniba.euromath.tools.ExceptionRegistry

public final class ExceptionRegistry
extends Object

The exception registry. Each producer that runs in its own thread and fails with an exception should record this exception in this registry right before its thread is terminated.

When producer registers an exception and dies, an IOException is thrown in piped read method (for example PipedInputStream.read() when working with byte streams). This causes the receiver to fail, register its exception and die, and so on. We can see that the very first (the oldest) exception is the cause of all other exceptions, hence it is sufficient to report just this 'cause'.

On the other hand, if receiver fails, registers the exception and terminates, this causes the producer to fail aswell, but we see that the first exception being reported is the real cause.

You should use the exception transferring mechanism like this:

final PipedExceptionReader consumer = new PipedExceptionReader();
final PipedWriter producer = new PipedWriter(reader);
new Thread() {
  public void run() {
    try {
      // write something to the producer, throw an exception if something fails.
      // this exception will be transparently rethrown in the consumer.
    } catch(Throwable t) {
      consumer.registry.registerException(t);
    }
  }
}
// you may now read from the consumer in this thread, any producer exceptions
// will be thrown in consumer's read methods.

Author:
Martin Vysny

Constructor Summary
ExceptionRegistry()
           
 
Method Summary
 Throwable getCause()
          Retrieves the cause (the oldest exception).
 List<Throwable> getRegistry()
          Retrieves the registry, oldest exception occupying index 0.
 void registerException(Throwable ex)
          Records an exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExceptionRegistry

public ExceptionRegistry()
Method Detail

registerException

public void registerException(Throwable ex)
Records an exception.

Parameters:
ex - exception to record.

getRegistry

public List<Throwable> getRegistry()
Retrieves the registry, oldest exception occupying index 0.

Returns:
unmodifiable list of exceptions, never null.

getCause

public Throwable getCause()
Retrieves the cause (the oldest exception).

Returns:
the oldest exception (the cause) or null if no exception was recorded.


Copyright 2003-2003-2006 null. All Rights Reserved.