English 中文(简体)
向代表提出例外的不专业方式
原标题:Idiomatic way to throw exceptions to a delegate

我有一个监视器类, 监测设备, 如果设备成功接收可用数据, 便报告设备。 这随时可能发生 。

客户端通过过路代表创建自己的监视器,启动并等待成功读取的数据或某种域特定例外类型(一种基本例外类型)

投放基本例外类型子类型并使客户能够单独对每个子类型作出反应的特异方式是什么?

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
    }
  }
}
最佳回答

那么,你为什么不处理你主机的例外 而不是监控舱?

如果这不是一种选择,那么你(至少)有两种选择:

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,...
  }
}

That way you don t have to rethrow the exception, just to catch it again. But this can get ugly if you have many subtypes to handle and here is where multidispatch comes in.

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
}

Now you can tend to each exception in its own method and is much easier to read and maintain. Having said that, I m not entirely sure if this falls under best practice.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签