簡體   English   中英

如何在 IE 8 中獲取 JavaScript 異常的堆棧跟蹤?

[英]How can I get the stack trace for a JavaScript exception in IE 8?

當 IE 8 拋出 JavaScript 異常時,如何查看其堆棧跟蹤?

例如,來自 jQuery 的以下代碼捕獲異常並重新拋出它。 在 Visual Studio (2012) 中調試,執行中斷,因為異常 ('e') 被 jQuery 捕獲,但我終究無法看到異常來源的堆棧跟蹤:

// resolve with given context and args
resolveWith: function( context, args ) {
    if ( !cancelled && !fired && !firing ) {
        firing = 1;
        try {
            while( callbacks[ 0 ] ) {
                callbacks.shift().apply( context, args );
            }
        }
        // We have to add a catch block for
        // IE prior to 8 or else the finally
        // block will never get executed
        catch (e) {
            throw e;
        }
        finally {
            fired = [ context, args ];
            firing = 0;
        }
    }
    return this;
}

我試過stacktrace.js庫,但當瀏覽器是 IE 8 時它似乎忽略了異常,只是回退到生成當前幀的堆棧跟蹤。

編輯:

從下面的屏幕截圖中可以看出,異常沒有與堆棧相關的屬性:

JavaScript 異常對象

看看這個鏈接: DIY javascript堆棧跟蹤

他的例子是我的IE8。

要在 Visual Studio 2012 中調試時查看異常來源的堆棧跟蹤,您可以使用調試器 object 中斷執行並檢查堆棧跟蹤。

以下是如何執行此操作的示例:

// resolve with given context and args
resolveWith: function( context, args ) {
    if ( !cancelled && !fired && !firing ) {
        firing = 1;
        try {
            while( callbacks[ 0 ] ) {
                callbacks.shift().apply( context, args );
            }
        }
        // We have to add a catch block for
        // IE prior to 8 or else the finally
        // block will never get executed
        catch (e) {
            // Break execution and inspect the stack trace
            Debugger.break();
            throw e;
        }
        finally {
            fired = [ context, args ];
            firing = 0;
        }
    }
    return this;
}

當捕獲到異常時,此代碼將中斷執行並打開 Visual Studio 調試器。 然后您可以檢查堆棧跟蹤並查看異常的來源。

請記住,這僅在 Visual Studio 調試器附加到進程時才有效。 如果沒有附加調試器,執行不會中斷,您將無法看到堆棧跟蹤。

這有幫助嗎?

這行不通!!

function getStackTrace(e) {
  var stack = [];
  while (e) {
    stack.push({
      fileName: e.fileName,
      lineNumber: e.lineNumber,
      name: e.name,
      message: e.message,
      stack: e.stack
    });
  }
  return stack;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM