【问题标题】:How write argument null exception and invalid exception?如何编写参数空异常和无效异常?
【发布时间】:2013-12-17 17:20:55
【问题描述】:

我正在尝试为Register CommandHandler编写一个类

public virtual void RegisterCommandHandler<T>(string messageType, Action<T, MetaData> handler)
    {
        _handlers.Add(messageType, (command, metadata) => handler((T)command, metadata));

    }

我要处理两个异常:

  1. 处理程序空异常
  2. 无效的处理程序异常

我尝试按照以下方式进行操作。但没有成功

 if (string.IsNullOrEmpty(handler.ToString())) 
       throw new ArgumentNullException("NullException","null value");

请帮我写验证 谢谢!

【问题讨论】:

    标签: c# unit-testing testing exception-handling nunit


    【解决方案1】:

    您应该直接检查处理程序是否为空。

    if (handler == null)
        throw new ArgumentNullException("handler cannot be null", "handler");
    

    您现在遇到问题的原因是,如果 handler 为 null,则 handler.ToString() 将抛出 NullReferenceException。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      • 2018-02-26
      • 2012-02-20
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      • 1970-01-01
      相关资源
      最近更新 更多