ways to avoid browser responses to JavaScript errors:
1. Use the Try-catch statement where the error may occur.
2, use the Window.onerror event processing statement, this way can accept all the errors Try-catch can not handle (ie, Firefox and Chrome only)
Try-catch statements
try{
Code that could cause the error
}catch (Error) {
Error handling code
}
If any code in the try block has an error, it exits the code execution immediately and then executes the catch block. At this point, the catch block receives an object containing the error message, different from other languages, in JS, even if you do not want to use the wrong object, you must give it a name, such as the error above. The information contained in this wrong object varies by browser. In common, the message property of the error messages is preserved. The Name property is also supported by the current browser.
1. Finally clause
The Try-catch statement is optional, but once the finally clause is used, the code will be executed anyway. Even if the Try-catch statement contains a return statement, the finally clause is bound to execute.
function testFinally() { try{ return0; }catch (error){ return1; }finally { return1; }}
Calling testfinally only returns 1, because if the code contains a finally clause, the RETUTN statement in either the try or catch statement block will be ignored.
2. Type of error
1. Error
2, Evalerror
3, Rangeerror
4, Referenceerror
5, SyntaxError
6, TypeError
7, Urierror
Where error is a base type, and other error types inherit from that type, errors of type error are rare, and the primary purpose of this base type is for developers to throw custom errors.
EvalerrorThe type of error is thrown when the eval () function is used instead of when an exception occurs.
RangeerrorType of error is triggered when the value exceeds the range
ReferenceerrorType of error is triggered when the object is not copied
SyntaxErrorThis error is caused when a syntactically incorrect JavaScript string is passed into the eval () function
TypeErrorTypes are often used in JavaScript, and the following conditions trigger this error:
1. Save the unexpected type in the variable.
2. Access to non-existent methods
3. The parameters passed to the function are not checked beforehand, and the resulting incoming type is inconsistent with the expected type.
UrierrorThe type is set to start when using encodeURI () or decodeURI (), and the URI is not in the correct format.
3. Reasonable use of Try-catch statement
When an error is sent in the Try-catch statement, the browser will assume that the error has been handled and no more errors will be thrown. Using Try-catch is best for handling errors that we cannot control. If you know your code is wrong, then using Try-catch is not appropriate.
Throw error
Throw is used to throw custom errors
If you are using a browser built-in error type, the emulation is a browser error, and each type of error constructor receives a parameter, which is the actual error message.
throw new error ("Something Error");
thrownewError("something error!");thrownewTypeError("Your type is wrong!");thrownewURIError("URI is wrong!")
When the THORW operator is encountered, the code stops executing immediately.
So we can get the following error by annotating the first sentence and the second line of code.
In addition to throwing browser-customized error types, we can also customize the error type.
<script> function MyError(message) { this.name = "MyError"; this.messages = message; } MyError.prototype = new Error(); throw new MyError("My Error");</script>
The purpose of throwing an error is to provide a message that the error occurred for a specific reason, and the purpose of the catch error is to prevent the browser from handling them by default.
Error Events
In any web browser, the OnError event handler does not create an events object, but it can receive 3 parameters: the error message, the URL where the error occurred, and the line number. Typically, only error messages are useful.
Window.onerror = function (message, URL, line) {
Code
}
If the event handler rises back false, you can prevent the browser from reporting the default behavior of the error.
Proper use of the Try-catch statement, there will be no error to the browser, and will not start the OnError event.
Common types of Errors
1. Type conversion error
2. Wrong data type
3. Communication error
Type conversion errors occur when using an operator or using other data type language structures that may automatically convert values, use congruent and not congruent as much as possible, and avoid type conversions
Data type error, detect data type where necessary, value of base type is detected using TypeOf, Object uses INSTANCEOD to detect
Many communication errors are due to incorrect URLs or data.
The following URL format is not correct
Http://www.baidu.com?wd=ni&tn=monline
Calling encodeURIComponent () for all strings behind WD solves this problem:
Http://www.baidu.com?wd%3Dni%26tn%3Dmonline
We can define a function that handles the query string:
<script> function addstringarg { if (Url.indexof () = =-1 ) {URL + = ; }else {URL += "&" ; } URL + = encodeuricomponent (name) + "=" + encodeuricomponent (value); } var url = "http://www.baidu.com" ; var newurl = Addstringarg (URL, "WD" , "ni&tn=monline" ); </script>
This function receives three parameters: the URL of the query string to append, the parameter name, and the parameter value.
We can use this method instead of manually stitching URLs.
JS error Handling