2011-01-19 14 views
0

1)テスト例外を作成しました。私は私のクラスにベース例外からキャストすることはできませんしかしC#例外の間にキャストされるユーザー定義の例外?

catch (Exception e) 
{ 
    TestException myOwnException = new TestException(e); 
    myOwnException.Info = "test"; 
    LogError(myOwnException); 
} 

:どこかに私はこのような何かを得たウェブサイト内の関数で

public class TestException : Exception 
{ 
    string info; 
    public string Info { get { return info; } set { info = value; } } 
    public TestException() { } 
    public TestException(string message) : base(message) { } 
    public TestException(string message, Exception inner) : base(message, inner) { } 
    protected TestException(
     System.Runtime.Serialization.SerializationInfo info, 
     System.Runtime.Serialization.StreamingContext context) 
     : base(info, context) { } 
} 

2)。 logErrorは、TestExceptionを予期します。

私はExceptionクラス内でこれを作成してみました。

public static implicit operator TestException(Exception e) 
{ 
    return new TestException(e); 
} 

しかし、私はちょうど得続ける(私はTestException myOwnException =電子を書くことができます):基底クラスへまたはから ユーザ定義の変換はありません許可されます。

catchステートメントから例外を私のTestExceptionクラスにキャストする方法はありますか? (私もTestException test =(TestException)eを試みましたが、エラーを返すだけです。

+0

LogErrorの定義とは – sukru

答えて

3

私はこれを短くして、この作業を行うことはできません。あなたはLogError Exceptionオブジェクトを受け入れる。任意の追加の状態が必要な場合(情報のような)、その後のlogError()メソッドの引数としてそれを追加します。または過負荷を加える。

0

私はベース 例外からキャストすることはできませんしかし、 logError はTestExceptionを予期します。

あなたの投稿のコードはExceptionからTestExceptionへのキャストは関係しません。 LogErrorにキャストはありますか?あなたが不明な場合は、LogErrorの内容を投稿してください。陰的なキャスト演算子を追加しようとせずに見る問題の正確な詳細も役立ちます。

関連する問題