This article mainly introduces the functions and examples of JS exception handling, that is, try .. catch statement. For more information, see the following common sense: When a Javascript script is executed in a browser, when a script error occurs and you do not manually capture exceptions, he will display a yellow exclamation mark under the browser. This is normal, and this is not the most important. The most important thing is that all JS Code below will be suspended if an error occurs, this is what we don't want to see. Therefore, it is better to add exception capture to scripts that are not accurate to ourselves.
1 clear the yellow exclamation mark:
The Code is as follows:
Window. onerror = {return true;} // The error is cleared on the surface, but the JS Code will be suspended.
2. Add try. catch to the code segment that is prone to errors and compatibility issues.
The Code is as follows:
Try {
Var a = "hello world ";
Document. Write ();
}
Catch (e ){
// What you need to do when an exception occurs
}