2012-02-24 7 views
0

(私はそれが無関係だった発見したので、初期コンテンツアウト、編集)、一般的な方法で消去.NET - 例外スタックトレースラインが

私のアプリケーションは 代表者に操作をラップし、ために過去である汎用RESTfulなHTTPクライアントを持っています私のWrapOperationメソッド(下記参照)。

例外が発生したときただし、スタックトレースにエントリが1つだけ含まれています:「スタックトレース6をテストする場合

private T WrapOperation<T>(String method, String path, Object requestObject, RestOperation<T> action) { 

     HttpWebRequest request; 
     RestTransaction txn = CreateRequest(method, path, requestObject, out request); 

     //////////////////////////// 

     try { 

      throw new WebException("testing stack trace 6"); 
      throw new Exception("testing stack trace 7"); 

      using(HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { 

       txn.GotResponse(response); 

       return action(txn, response); 
      } 

     } catch(WebException wex) { 
      // NOTE: When exceptions are caught, they're missing the first few entries of the stack trace 
      // And appear as though "WrapOperation[T]" (sic) is the entrypoint. Why is this? 


      if(wex.Response != null) { 

       HttpWebResponse response = (HttpWebResponse)wex.Response; 

       txn.GotResponse(wex, response); 

       CopyResponseToMemoryStream(response, txn).Dispose(); 

      } else { 

       txn.GotResponse(wex); 
      } 

      // NOTE: However, when these RestExceptions are caught (by WrapOperation's caller), their stack trace is complete and shows the entire trace 
      throw new RestException("WebExeption during GetResponse.", txn, wex); 

     } catch(Exception ex) { 

      txn.GotResponse(ex); 

      // NOTE: However, when these RestExceptions are caught (by WrapOperation's caller), their stack trace is complete and shows the entire trace 
      throw new RestException("Non-WebException during GetResponse.", txn, ex); 
     } 

    } 

at MyProject.Client.RestClient.WrapOperation[T](String method, String path, Object requestObject, RestOperation`1 action) in D:\{fileName}\RestClient.cs:line 196 

を私はちょうどこの問題のコードを削減しました例外がスローされます。catch(WebException wex)によってキャッチされると、wexのスタックトレースには1つのエントリしか含まれません。

これはなぜですか?

+0

例外コードは実際にどのように見えますか?例外をチェーンの下に渡すために 'throw ex'または単に' throw'していますか? – asawyer

+0

欠落している中間メソッドのコードを投稿して推測しにくいようにしました。ここに少なくとも2つの場合があります。タスクによって実行されるメソッドのみが表示され、例外トレースはスレッドベースです。また、ジッタオプティマイザは小さなメソッドのインラインコードを生成するので、それらを見ることはできません。例外はスタックフレームベースです。 [MethodImpl]を使用して最適化を抑制します。 –

+0

スレッドのエントリポイントはどのような方法ですか?それはDoAsync(Action)ですか?非同期タスクを作成、起動、実行するコードを表示してください。 –

答えて

0

元の例外をカスタム例外クラスにラップするようです。例外を読み取ってスタックトレースを調べるときは、完全なトレースを取り戻すために内側の例外ラインを調べなければなりません。

編集 -

例外がスローされ、スタックトレースを開始するには、そのようなスタックトレースが正しいこと、および問題の私の元の記述が正しいです。

いつでも新しい例外をスローすると、その例外のスタックトレースはその時点から開始されます。あなたの(不図示)のいずれかの方法は、例外をキャッチして、そのようにそれを再スローされた場合

//throwing the exception resets the stack trace! 
//the next catch will see the exception as coming from this line, and 
//will need to look at the inner exception at that point to see the 
//original exception! I Think! I have no idea what RestException is, 
//only that your passing the original exception ex into it as a parameter. 
throw new RestException("Non-WebException during GetResponse.", txn, ex); 
+0

内部例外(RestExceptionsがインスタンス化されたときに割り当てられたもの)には最初のスタックフレームがありません - 例外をラッピングしているという事実は無関係です。 – Dai

0

throw ex; 

あなたの元の例で

再び、ここでの内部例外を調べスタックトレースはリセットされ、再スローポイントで開始されます。

throw; 

スタックトレースを維持します。

関連する問題