Error Handler

 public class DiskService{
private Writer diskWriter = ...;
private final ErrorHandler ioErrorHandler = new ErrorHandler();

...

public void writeToDisk(byte[] data){ try{ diskWriter.write(data); } catch(IOException error){ ioErrorHandler.toss(error); } }

public ErrorHandler onIOError(){ return ioErrorHandler; } }

public class ErrorHandler{ private List errorMethodObjects = new List();

public void toss(Throwable error){ Iterator eachMethod = errorMethodObjects.iterator(); while(eachMethod.hasNext()){ ((MethodObject) eachMethod.next()).invoke(error); //or however you decide to call it } }

public void doThis(MethodObject errorHandlingMethodObject){ errorMethodObjects.add(errorHandlingMethodObject); }

}

public class ErrorHandledMethod? extends MethodObject{ public final Object invoke(Object parameter){ try{ task(parameter); } catch(AbortTaskMessage? abort){ throw abort; } catch(RetryTaskMessage? retry){ invoke(retry.getParameter()); } catch(IgnoreFailureMessage? ignore){ return ignore.getResult(); } }

public abstract void task(Object parameter); }

public class Client extends ErrorHandledMethod?{ DiskService service = new DiskService(); { service.onIOError().doThis(new MethodObject(){ public void invoke(Object ioError){ throw new AbortTaskMessage?(); } }); } ... public void task(Object parameter){ service.write((byte[]) parameter); } ... }

Discuss :)


EditText of this page (last edited March 22, 2003) or FindPage with title or text search