2012-02-20 11 views
1

ロギングを指定してキャッチするために新しい例外タイプを作成したい。しかし、私はint値をctorに渡したいと思います。どうやってするの?試してみます:新しい例外タイプを作成する

public class IncorrectTimeIdException : Exception 
{ 
    public IncorrectTimeIdException(int TypeID) : base(message) 
    { 
    } 
} 

コンパイル時にエラーが発生しました。

+5

'message'は定義されていません。したがって、ベース(メッセージ)の代わりに試しベース( "何か間違っている") – mshsayem

+4

例外メッセージ(意図しない言い訳)は、このケースで何が間違っているかを正確に教えてください。 – BrokenGlass

+2

http://blog.gurock.com/articles/creating-カスタム例外 - インドットネット/新しい例外タイプのハウツー – ken2k

答えて

2
public class IncorrectTimeIdException : Exception 
{ 
    private void DemonstrateException() 
    { 
     // The Exception class has three constructors: 
     var ex1 = new Exception(); 
     var ex2 = new Exception("Some string"); // <-- 
     var ex3 = new Exception("Some string and InnerException", new Exception()); 

     // You're using the constructor with the string parameter, hence you must give it a string. 
    } 

    public IncorrectTimeIdException(int TypeID) : base("Something wrong") 
    { 
    } 
} 
+0

クイック質問。私の例外を構築するとき、私はDemonstrateExceptionと同様のメソッドを呼び出すか、それとも自然に行われますか? –

1

メッセージには、 - messageが定義されていないことが正確に表示されます。

あなたは例外作成時にユーザメッセージを供給することを可能にする代わりにこれを試してみてください:

public IncorrectTimeIdException(string message, int TypeID) : base(message) 
{ 
} 

// Usage: 
throw new IncorrectTimeIdException("The time ID is incorrect", id); 

または代わりに、この、メッセージなしで例外を作成します。

public IncorrectTimeIdException(int TypeID) 
{ 
} 

それとも最終的には、このこれは、あらかじめ定義されたメッセージで例外を作成します。

public IncorrectTimeIdException(int TypeID) : base("The time ID is incorrect") 
{ 
} 

Ifクラスに複数のコンストラクターを宣言することもできるので、あらかじめ定義されたメッセージを使用するコンストラクターと同時にそのメッセージをオーバーライドできるコンストラクターを提供することができます。

2

いくつかの追加のデータ(あなたの場合はタイプID)を持ち、カスタム例外を作成するためのすべての "ルール"に従うカスタム例外クラスを作成するためのコードです。例外クラスと、わかりにくいカスタムデータフィールドの名前を自由に変更することができます。

using System; 
using System.Runtime.Serialization; 

[Serializable] 
public class CustomException : Exception { 

    readonly Int32 data; 

    public CustomException() { } 

    public CustomException(Int32 data) : base(FormatMessage(data)) { 
    this.data = data; 
    } 

    public CustomException(String message) : base(message) { } 

    public CustomException(Int32 data, Exception inner) 
    : base(FormatMessage(data), inner) { 
    this.data = data; 
    } 

    public CustomException(String message, Exception inner) : base(message, inner) { } 

    protected CustomException(SerializationInfo info, StreamingContext context) 
    : base(info, context) { 
    if (info == null) 
     throw new ArgumentNullException("info"); 
    this.data = info.GetInt32("data"); 
    } 

    public override void GetObjectData(SerializationInfo info, 
    StreamingContext context) { 
    if (info == null) 
     throw new ArgumentNullException("info"); 
    info.AddValue("data", this.data); 
    base.GetObjectData(info, context); 
    } 

    public Int32 Data { get { return this.data; } } 

    static String FormatMessage(Int32 data) { 
    return String.Format("Custom exception with data {0}.", data); 
    } 

} 
関連する問題