如何捕獲所有未處理的 JavaScript 異常?
要捕獲所有未處理的 JavaScript 異常,請使用 window.error。onerror 事件處理程式提供三項資訊以標識確切的錯誤性質:
- 錯誤訊息 - 瀏覽器將顯示的給定錯誤的相同訊息
- URL - 發生錯誤的檔案
- 行號 - 給定 URL 中導致錯誤的行號
示例
你可以嘗試執行以下程式碼來捕獲未處理的異常:
<html>
<head>
<script>
<!--
window.onerror = function (msg, url, line) {
alert("Message : " + msg );
alert("url : " + url );
alert("Line number : " + line );
}
//-->
</script>
</head>
<body>
<p>Click the following to see the result:</p>
<form>
<input type = "button" value = "Click Me" onclick = "myFunc();" />
</form>
</body>
</html>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP