C# 如何抛出异常并添加包含键和值的消息?

C# 如何抛出异常并添加包含键和值的消息?,c#,exception,C#,Exception,我有这样的方法: public IDictionary<string, string> Delete(Account account) { try { _accountRepository.Delete(account); } catch { _errors.Add("", "Error when deleting account"); } return _errors; } public IDictionary<string, string>

我有这样的方法:

public IDictionary<string, string> Delete(Account account)
{
    try { _accountRepository.Delete(account); }
    catch { _errors.Add("", "Error when deleting account"); }
    return _errors;
}

public IDictionary<string, string> ValidateNoDuplicate(Account ac)
{
    var accounts = GetAccounts(ac.PartitionKey);
    if (accounts.Any(b => b.Title.Equals(ac.Title) &&
                            !b.RowKey.Equals(ac.RowKey)))
        _errors.Add("Account.Title", "Duplicate");
    return _errors;
}
有人能告诉我如何抛出异常并传递包含键和值的消息吗。在这种情况下,键将是
,值将是
“删除帐户时出错”

也在调用此函数的方法中。我如何捕捉异常


我是否需要创建自己的类并基于该类以某种方式抛出异常?

创建自己的异常,然后抛出它

public class RepositoryException : Exception
{
    public RepositoryException() : base()
    {
    }

    public RepositoryException(string key, string value) : base()
    {
        base.Data.Add(key, value);
    }

    public RepositoryException(string message) : base(message)
    {
    }

    public RepositoryException(string message, Exception innerException) : base(message, innerException)
    {
    }
}


public Boolean Delete(Account account)
{
    try 
    { 
        _accountRepository.Delete(account); 
        return true;
    }
    catch (Exception ex)
    { 
        throw new RepositoryException("", "Error when deleting account");            
        // throw new RepositoryException("Error when deleting account", ex);
        // OR just
        // throw new RepositoryException("Error when deleting account");
    }
}

异常
类有一个属性,它是键/值对的字典

IDictionary<string, string> errors;
...

if (errors.Count > 0)
{
    Exception ex = ... construct exception of the appropriate type
    foreach(string key in _errors.Keys)
    {
        ex.Data.Add(key, _errors[key]);
    }
    throw ex;
}
i字典错误;
...
如果(errors.Count>0)
{
Exception ex=…构造适当类型的异常
foreach(字符串输入_errors.Keys)
{
例如数据添加(键,_错误[key]);
}
掷骰子;
}
请注意,通常认为使用可序列化的异常是一种良好的做法,因此放入数据字典的对象也应该是可序列化的。在您的示例中,您只需要输入字符串,这样就可以了

我是否有必要创建自己的类并基于该类抛出异常

创建您自己的自定义异常类肯定不是必要的,也可能不可取。该报告提供了有关的指导方针


一般来说,您应该更喜欢使用现有的异常类型之一,除非您有一个错误条件,可以通过与现有异常类型不同的编程方式进行处理。

创建您自己的异常类,该类可以保存您需要的数据:

public class AccountException : ApplicationException {

  public Dictionary<string, string> Errors { get; set; };

  public AccountException(Exception ex) : base(ex) {
    Errors = new Dictionary<string, string>();
  }

  public AccountException() : this(null) {}

}
调用方法时,捕获异常类型:

try {
  Delete(account);
} catch(AccountException ex) {
  // Handle the exception here.
  // The ex.Errors property contains the string pairs.
  // The ex.InnerException contains the actual exception
}

您可以抛出自己的异常,而不是

\u错误。添加(“,”删除帐户时出错”)

因此,每个
\u错误。Add(…)
将替换为以下内容

抛出新的MyAppException(键、值)

上面解释了如何创建自己的异常类。因此,可以为异常对象提供

您应该知道将捕获哪种异常类型

try {
  Delete(account);
} catch(NullPointerException ex) {
  throw new MyAppException(key, value);
}
现在,在调用方方法(外部方法)中,您可以只捕获您的异常

try {
  _accountRepository.Delete(account);
} catch(MyAppException ex) {
  //exception handle logic
}

私人词典(private Dictionary)错误;;但我的问题是我需要抛出一个带有两个字符串的异常。一个键和一个值。例如,当我需要将信息“Account.Title”作为键传递,“Duplicate”作为值传递时,如何使用您的方法抛出异常?我想我需要做的是使用一个类创建我自己类型的异常,但我不知道如何做。谢谢Charandeep。您在哪里使用ServiceException?我看到它是创建的,但没有使用。另外,如何从外部方法中的异常中获取数据呢?哎呀,我的坏消息,它实际上是构造函数重载的一种。我已经编辑了它。事实上,作者提供信息的博文谈到了属性
异常。Data
:“基本异常类包含一个名为Data的属性,它是一个打开的字典。任何引发异常的代码都可以自由地将自定义数据添加到异常中。然后处理代码可以访问这些数据。”,Jared Par博客文章的一句话总结道:“如果希望开发人员对问题采取纠正措施或进行事后调试,则只应创建一个新的异常。”
try {
  Delete(account);
} catch(NullPointerException ex) {
  throw new MyAppException(key, value);
}
try {
  _accountRepository.Delete(account);
} catch(MyAppException ex) {
  //exception handle logic
}