1. PurposeNormally, if an error occurs, the script stops immediately and prints the error to the console. With this statement, you can catch errors and perform reasonable operations, allowing the program to continue executing. 2. Grammartry { // Code... } catch (err) { //err is an object with detailed information about the error // Error capture, the above code will go to this code block when an error is reported, and will not stop running} finally { //It always executes regardless of whether an exception is thrown or caught} This statement can be nested 3. Practice If we don't know how to handle it, we The Used to throw a user-defined exception. Execution of the current function will be stopped (the statements after throw will not be executed) and control will be passed to the first Catch block in the call stack. If there is no catch block in the caller function, the program will terminate. Example: throw "Error1"; // throws an exception with a string value throw 4; // throws an exception with an integer value 4 /*There are many built-in standard error constructors in JavaScript: Error, SyntaxError, ReferenceError, TypeError, etc. We can also use them to create error objects. */ let error = new Error("Things happen o_O"); alert(error.name); // Error alert(error.message); // Things happen o_O //json exception try { JSON.parse("{ bad json o_O }"); } catch(e) { alert(e.name); // SyntaxError alert(e.message); // Unexpected token b in JSON at position 2 } Replenish: The code in the try block may throw three kinds of exceptions: Some students may not know the meaning of these three, so let me explain them here. Both of these are global objects. The global object itself does not contain any methods, but it inherits some methods through the prototype chain. The instanceof operator is used to determine the type of error:
Examples: catch (e) {//The following are the parameter properties of this object console.log(e instanceof TypeError); // true console.log(e.message); // "Describe this error" console.log(e.name); // "TypeError" console.log(e.fileName); // "The name of the file where the code that caused the exception is located" console.log(e.lineNumber); // The line number of the code that caused the exception} This concludes the article on JavaScript error handling try..catch...finally + covering throw+TypeError+RangeError. For more relevant JavaScript error handling content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: What is ssh port forwarding? What's the use?
>>: CSS realizes div completely centered without setting height
A record of an online MySQL transaction problem L...
The specific code for sending emoticons in the vu...
Life cycle classification Each component of vue i...
Operating system: windowns10_x64 Python version: ...
GitHub address, you can star it if you like it Pl...
Execute the command: docker run --name centos8 -d...
mysql-5.7.19-winx64 installation-free version con...
The mysql service is started, but the connection ...
For example: Copy code The code is as follows: <...
Author | Editor Awen | Produced by Tu Min | CSDN ...
What is DOM? With JavaScript, you can reconstruct...
Underlining in HTML used to be a matter of enclos...
Three times of memorization allows you to remembe...
Today I will share with you a good-looking counte...
Cocos Creator version: 2.3.4 Demo download: https...