[英]How can I get specific info from exception to add it in my own message?
[英]How can I throw an exception and add in my own message containing a key and a value?
我的方法看起来像这样:
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;
}
我想更改此方法,以便它返回一个bool,因此如果有错误而不是:它会引发异常:
_errors.Add("", "Error when deleting account");
有人可以向我解释如何抛出异常并传递包含密钥和值的消息。 在这种情况下,密钥将是""
,值将是"Error when deleting account"
。
同样在调用它的方法中。 我怎么能抓住这个例外?
我是否有必要创建自己的类并以某种方式基于此类抛出异常?
Exception
类有一个Data
属性,它是键/值对的字典。
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;
}
请注意,通常认为使用Serializable的异常是一种好习惯,因此放入Data字典的对象也应该是可序列化的。 在你的例子中,你只是放入字符串,所以你会没事的。
我是否有必要创建自己的类并以某种方式基于此类抛出异常?
当然没有必要创建自己的自定义Exception类,可能并不可取。 Exception的MSDN设计指南提供了有关选择要抛出的Exception类型的指南。
通常,您应该更喜欢使用现有的Exception类型之一,除非您有一个错误条件,可以以与现有Exception类型不同的方式以编程方式处理。
创建自己的异常类,它可以保存您需要的数据:
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) {}
}
在您的方法中,您可以抛出异常。 不要返回错误状态,由异常处理。
不要丢弃您在方法中获得的InnerException
,将其包含在InnerException
,以便它可以用于调试。
public void Delete(Account account) {
try {
_accountRepository.Delete(account);
} catch(Exception ex) {
AccountException a = new AccountException(ex);
a.Errors.Add("", "Error when deleting account");
throw a;
}
}
public void ValidateNoDuplicate(Account ac) {
var accounts = GetAccounts(ac.PartitionKey);
if (accounts.Any(b => b.Title.Equals(ac.Title) &&
!b.RowKey.Equals(ac.RowKey))) {
AccountException a = new AccountException();
a.Errors.Add("Account.Title", "Duplicate");
throw a;
}
}
调用方法时,会捕获异常类型:
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
}
创建自己的异常,然后抛出它。
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");
}
}
您可以抛出自己的异常而不是
_errors.Add("", "Error when deleting account");
所以每个_errors.Add(..)
将被替换为类似的东西
throw new MyAppException(key, value);
上面解释了如何创建自己的异常类。 因此,您使用键和值提供异常对象。
您应该知道要捕获的异常类型
try {
Delete(account);
} catch(NullPointerException ex) {
throw new MyAppException(key, value);
}
现在,在您的来电者的方法(外的方法),你只能抓住你的例外。
try {
_accountRepository.Delete(account);
} catch(MyAppException ex) {
//exception handle logic
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.