Professional JavaScript for Web Developers 第四版学习笔记 CHAPTER 21:Error Handling and Debugging

欢欢欢欢 发表于 2021-10-27 09:17

Browser Error Reporting 824
Desktop Consoles 824
Mobile Consoles 824
Error Handling 825
The try-catch Statement 825
 The finally Clause 826
 Error Types 826
 Usage of try-catch 828
Throwing Errors 829
 When to Throw Errors 830
 Throwing Errors versus try-catch 831
The error Event 831
Error-Handling Strategies 832
Identify Where Errors Might Occur 833
 Static Code Analyzer 833
 Type Coercion Errors 833
 Data Type Errors 835
 Communication Errors 837
Distinguishing between Fatal and Nonfatal Errors 838
Log Errors to the Server 839
Debugging Techniques 840
Logging Messages to a Console 840
Understanding the Console Runtime 841
Using the JavaScript Debugger 842
Logging Messages to the Page 842
Shimming Console Methods 843
Throwing Errors 843
Common Legacy Internet Explorer Errors 844
Invalid Character 844
Member Not Found 845
Unknown Runtime Error 845
Syntax Error 845
The System Cannot Locate the Resource Specified 846
Summary 846

---------------------------------------------

function logError(sev, msg) {
 let img = new Image(),
      encodedSev = encodeURIComponent(sev),
      encodedMsg = encodeURIComponent(msg);
 img.src = 'log.php?sev=${encodedSev}&msg=${encodedMsg}';
}

通过Image向服务器发送日志。

---------------------------------------------

function assert(condition, message) {
 if (!condition) {
  throw new Error(message);
 }
}

function divide(num1, num2) {
 assert(typeof num1 == "number" && typeof num2 == "number",
  "divide(): Both arguments must be numbers.");
 return num1 / num2;
}

assert函数是一种抛异常的新方法,很新颖。

----------------------------------------------

When an invalid character is detected in a JavaScript file, IE throws the "invalid character" error.