2011-12-17 58 views
4

我看起來像這樣的方法:如何拋出一個異常並添加我自己的消息中包含一個鍵和一個值?

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; 
} 

我想改變這個方法,讓它返回一個布爾值,因此,如果有錯誤,而不是拋出一個異常:

_errors.Add("", "Error when deleting account"); 

有人可以向我解釋我如何拋出一個異常並傳遞一個包含一個鍵和一個值的消息。在這種情況下,密鑰將是"",值將是"Error when deleting account"

也在調用此方法。我將如何捕捉異常?

是否有必要讓我自己創建類並以某種方式拋出基於此類的異常?

+0

_errors是什麼類型? – 2011-12-17 07:13:49

+0

private Dictionary _errors; – 2011-12-17 07:28:32

回答

5

創建自己的異常類,可以保存數據,您需要:

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,以便它可用於調試。

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 
} 
1

創建您自己的異常,然後拋出它。

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"); 
    } 
} 
5

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; 
} 

注意,它通常被認爲是使用可序列,讓你把數據字典中的對象也應該是可序列化異常好的做法。在你的例子中,你只是把字符串,所以你會沒事的。

是否有必要讓我自己創建類並以某種方式拋出基於此類的異常?

肯定不是必要創建您自己的自定義Exception類,可能並不理想。 MSDN design guidelines for Exceptions給出了關於choosing which Exception type to throw的指導原則。

一般而言,除非您有一個錯誤條件,它可以通過與現有的異常類型不同的方式以編程方式處理,否則您應該更喜歡使用現有的異常類型之一。

1

你可以扔掉,而不是

_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 
} 
相關問題