久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

堆棧跟蹤上的錯誤行號

Wrong line number on stack trace(堆棧跟蹤上的錯誤行號)
本文介紹了堆棧跟蹤上的錯誤行號的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有這個代碼

try
{
  //AN EXCEPTION IS GENERATED HERE!!!
}
catch  
{
   SqlService.RollbackTransaction();
   throw;
}

上面的代碼在這段代碼中被調用

Code above is called in this code

try
{
  //HERE IS CALLED THE METHOD THAT CONTAINS THE CODE ABOVE
}
catch (Exception ex)
{
   HandleException(ex);
}

作為參數傳遞給方法HandleException"的異常包含堆棧跟蹤中throw"行的行號,而不是生成異常的實際行.有誰知道為什么會發生這種情況?

The exception passed as parameter to the method "HandleException" contains the line number of the "throw" line in the stack trace instead of the real line where the exception was generated. Anyone knows why this could be happening?

EDIT1好的,謝謝大家的回答.我把內扣換成了

EDIT1 Ok, thanks to all for your answers. I changed the inner catch for


catch(Exception ex)
{
    SqlService.RollbackTransaction();
    throw new Exception("Enrollment error", ex);
}

現在我在堆棧跟蹤中有正確的行,但我必須創建一個新異常.我希望找到更好的解決方案:-(

Now I have the correct line on the stack trace, but I had to create a new exception. I was hoping to find a better solution :-(

EDIT2也許(如果你有 5 分鐘的時間)你可以嘗試這個場景來檢查你是否得到相同的結果,重新創建不是很復雜.

EDIT2 Maybe (if you have 5 minutes) you could try this scenario in order to check if you get the same result, not very complicated to recreate.

推薦答案

是的,這是異常處理邏輯的限制.如果一個方法包含多個拋出異常的 throw 語句,那么您將獲得最后一個拋出異常的行號.此示例代碼重現了此行為:

Yes, this is a limitation in the exception handling logic. If a method contains more than one throw statement that throws an exception then you'll get the line number of the last one that threw. This example code reproduces this behavior:

using System;

class Program {
    static void Main(string[] args) {
        try {
            Test();
        }
        catch (Exception ex) {
            Console.WriteLine(ex.ToString());
        }
        Console.ReadLine();
    }
    static void Test() {
        try {
            throw new Exception();  // Line 15
        }
        catch {
            throw;                  // Line 18
        }
    }
}

輸出:

System.Exception: Exception of type 'System.Exception' was thrown.
   at Program.Test() in ConsoleApplication1Program.cs:line 18
   at Program.Main(String[] args) in ConsoleApplication1Program.cs:line 6

解決方法很簡單,只需使用輔助方法來運行可能引發異常的代碼.

The work-around is simple, just use a helper method to run the code that might throw an exception.

像這樣:

static void Test() {
    try {
        Test2();                // Line 15
    }
    catch {
        throw;                  // Line 18
    }
}
static void Test2() {
    throw new Exception();      // Line 22
}

這種尷尬行為的根本原因是 .NET 異常處理建立在操作系統對異常的支持之上.稱為 SEH,Windows 中的結構化異常處理.這是基于堆棧幀的,每個堆棧幀只能有一個活動異常..NET 方法有一個堆棧幀,與方法內的范圍塊數量無關.通過使用輔助方法,您可以自動獲得另一個可以跟蹤其自身異常的堆棧幀.當方法包含 throw 語句時,抖動還會自動抑制內聯優化,因此無需顯式使用 [MethodImpl] 屬性.

The underlying reason for this awkward behavior is that .NET exception handling is built on top of the operating system support for exceptions. Called SEH, Structured Exception Handling in Windows. Which is stack-frame based, there can only be one active exception per stack frame. A .NET method has one stack frame, regardless of the number of scope blocks inside the method. By using the helper method, you automatically get another stack frame that can track its own exception. The jitter also automatically suppresses the inlining optimization when a method contains a throw statement so there is no need to explicitly use the [MethodImpl] attribute.

這篇關于堆棧跟蹤上的錯誤行號的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Is there a C# library that will perform the Excel NORMINV function?(是否有執行 Excel NORMINV 函數的 C# 庫?)
Select x random elements from a weighted list in C# (without replacement)(從 C# 中的加權列表中選擇 x 個隨機元素(無需替換))
Create a summary description of a schedule given a list of shifts(給定輪班列表,創建時間表的摘要描述)
C# Normal Random Number(C# 普通隨機數)
Standard deviation of generic list?(通用列表的標準偏差?)
AsyncCTP: Creating a class that is IAwaitable(AsyncCTP:創建一個 IAwaitable 的類)
主站蜘蛛池模板: 人和拘一级毛片c | 午夜精品久久久久久久久久久久久 | 国产成人精品一区二区三 | 888久久久 | 365夜爽爽欧美性午夜免费视频 | 免费的黄色片子 | 久久久蜜桃 | 夜夜骚 | 久久精品日产第一区二区三区 | 99这里只有精品视频 | 亚洲第一av | www.久草.com | av av在线| 久久久久久久久淑女av国产精品 | 久久男人 | 久久久www成人免费精品 | 99精品久久久 | 国产精品一区在线观看 | 久久精品国产一区二区电影 | 罗宾被扒开腿做同人网站 | 日韩精品一区二区三区在线播放 | 欧美色综合网 | 狠狠色综合欧美激情 | 欧美黑人一区二区三区 | 日日操日日干 | 久久久精品一区二区 | 欧美一区二区综合 | www.欧美.com | 婷婷精品 | 人人人人干 | 日韩高清在线观看 | 欧美电影一区 | 欧美精品一区三区 | 欧美精品一区二区三区一线天视频 | 亚州精品天堂中文字幕 | 夜夜爽99久久国产综合精品女不卡 | 国产精品久久久久久久久大全 | 久草免费在线视频 | 精品欧美一区二区在线观看视频 | 亚洲精品丝袜日韩 | 亚洲黄色高清视频 |