2014-02-25 50 views
1
[Serializable] 
public class CommonsDbException : Exception 
{  
    public CommonsDbException() 
    { 
    } 

    public CommonsDbException(string message, object[] args) 
    { 
     base.message = string.Format(message, args); 
    } 

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

    protected CommonsDbException(SerializationInfo info, StreamingContext context) : base(info, context) 
    { 
    } 
} 

擴展的異常與新的消息

public CommonsDbException(string message, object[] args) 
{ 
    base.message = string.Format(message, args); 
} 

是無效的,我不能base.Message設置信息。

我想創建新消息,我該怎麼辦。父親的消息沒有安裝者。

+0

@BlackICE這是不對的。構造函數不會被繼承。 (如果給出零實例構造函數,編譯器會爲您創建一個默認實例構造函數,但這是另一回事。) –

回答

5

您需要鏈的構造:

public CommonsDbException(string message, object[] args) : base(string.Format(message, args)) 
{ 

} 

這就要求在祖先Exception(string message)構造。

關於您應該閱讀的構造函數,有一篇文章here on MSDN

+0

感謝您的快速回復。我會檢查文檔。 Thx – mbrc

+0

我認爲將params添加到最終參數中是有用的(儘管不是他的問題的一部分)。 –

0

你嘗試

public CommonsDbException(string message, object[] args) : base(string.Format(message, args), null) 
{ 
}