1

我有一个监视器类,它监视设备并报告该设备是否成功接收到可用数据。这可能随时发生。

客户端通过传递委托创建自己的监视器,启动它并等待成功读取数据或一种特定于域的异常类型(一个基本异常类型)

抛出基本异常类型的子类型并使客户端能够单独响应每个子类型的惯用方式是什么?

public class MyMonitor
{
  private SuccessHandler _successHandler;
  private ErrorHandler _errorHandler;
  public delegate void SuccessHandler(MyDTO result);
  public delegate void ErrorHandler(MyBaseException exception);

  public MyMonitor(SuccessHandler successHandler, ErrorHandler errorHandler) {
    _successHandler = successHandler;
    _errorHandler = errorHandler;
  }

  public void start() {
    try {
      _successHandler(new MyDTP().doSomethingRisky());
    } catch(Exception e) {
      _errorHandler(e);
    }
  }
}

public class Client {
  static void Main(string[] args) {
    MyMonitor monitor = new MyMonitor(new MyMonitor.SuccessHandler(handleSuccess), new MyMonitor.ErrorHandler(handleException));
    monitor.start();
  }

  static void handleSuccess(MyDTO result) {
    // do something with result
  }

  static void handleException(MyBaseException e) {
    try {
      throw e;
    } catch(UserException mbe) {
      // present message to user
    } catch(DataNotFoundException se) {
      // log error and show generic error message
    } catch(UnexpectedException ue) {
      // log error and try to hide it from the user
    }
  }
}
4

1 回答 1

2

那么,为什么不在主类而不是监视器类中处理异常呢?

如果这不是一个选项,你有(至少)两个选择:

static void handleException(MyBaseException e)
{
  if (e is UserException)
  {
    // present message to user
  }
  else if (e is DataNotFoundException)
  {
    // log error and show generic error message
  }
  elseif (e is UnexpectedException)
  {
    // log error and try to hide it from the user
  }
  else
  {
    // might want to rethrow the exception, do a general handling,...
  }
}

这样您就不必重新抛出异常,只需再次捕获它。但是,如果您有许多子类型要处理,这可能会变得很难看,这就是 multidispatch 的用武之地。

static void HandleException(MyBaseException e)
{
  HandleSubException((dynamic)e);
}

static void HandleSubException(MyBaseException e)
{
    // might want to rethrow the exception, do a general handling,...
}

static void HandleSubException(DataNotFoundExceptione)
{
    // log error and show generic error message
}

static void HandleSubException(UnexpectedException e)
{
    // log error and try to hide it from the user
}

static void HandleSubException(UserExceptione)
{
    // present message to user
}

现在,您可以在自己的方法中处理每个异常,并且更容易阅读和维护。话虽如此,我不完全确定这是否属于最佳实践。

于 2012-05-26T07:32:20.647 回答