Preface: In our actual programming, throwing exceptions (code errors) is the most normal thing, but how to handle exceptions varies from person to person. Some people encounter exceptions and usually solve the exception or hide it in some way; but in In this article we will introduce JavaScript processing 1. Concept 1.1 What are errors and exceptions?The so-called error is a state in the programming process that prevents the program from running normally, also known as an exception. In Through the exception handling statements provided by 1.2 Classification of anomaliesIn actual development, exceptions can be mainly divided into the following three types:
2. Exception handling 2.1try...catch statementThe try...catch statement is a standard way to handle exceptions in JavaScript. The syntax structure is as follows: try { // Code block for testing } catch(err) { // Code block to handle errors } parameter:
The sample code is as follows: try { // Code block used to test if there is any error console.log(v) // v is not defined at this time and an exception will be thrown } catch (error) { // Throwing an exception will execute this code block console.log('The above code has an error') }
2.2 finally statement The The syntax structure is as follows: try { // Code block for testing } catch(err) { // Code block to handle errors } finally { // Code block that executes regardless of the try catch result} The sample code is as follows: // var v try { // Code block used to test if there is any error console.log(v) // v is not defined at this time and an exception will be thrown } catch (error) { // Throwing an exception will execute this code block console.log('The above code has an error') finally console.log('I must be executed') } 2.3throw statement The The syntax format is as follows: throw expression;
Use the The sample code is as follows: // throw "error" // output error throw false // output false Of course, 3. Error Object An error object can be created through the Error constructor. When a runtime error occurs, an instance of The Error object is mainly used as the base object for user-defined exceptions. In addition to the Error object, JavaScript also provides the following predefined types of errors:
Error has two main properties:
The syntax format for creating an instance of an Error object is as follows: new Error([message) parameter: The creation syntax of other predefined types is the same as Error 3.1 Custom Exception Types If the exception type provided by Let's first look at the properties and methods provided in As shown below: The sample code is as follows: function MyError(message) { this.message = message this.name = 'MyError' /* * Error.captureStackTrace(targetObject[, constructorOpt]) * Parameter targetObject -> represents an object * Parameter constructorOpt -> represents the constructor of the object * Create a .stack property on targetObject, and the call returns a string of the location where Error.captureStackTrace() is called. */ Error.captureStackTrace(this, MyError) } MyError.prototype = new Error() MyError.prototype.constructor = MyError // * In the node.js environment, new Error will directly throw an exception. It is not applicable to the node.js environment. // function MyError(message) { // this.name = 'MyError'; // this.message = message || 'Default Message'; // this.stack = (new Error()).stack; // } // MyError.prototype = Object.create(Error.prototype); // MyError.prototype.constructor = MyError; try { throw new MyError('wrong') } catch (e) { console.log(e) } Conclusion: Exception handling in JavaScript generally only does two things in actual development:
This is the end of this article about JavaScript advanced custom exceptions. For more relevant JavaScript custom exception content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to solve the problem of left alignment of the last line in flex layout space-between
A reader contacted me and asked why there were pr...
The <link> tag defines the relationship bet...
Preface To solve the single point of failure, we ...
Have you ever had the need to compute a very larg...
Many people use Linux Homebrew. Here are three ti...
Here is a text hovering and jumping effect implem...
There are four types of positioning in CSS, which...
Copy code The code is as follows: <!DOCTYPE ht...
A simple Linux guessing game source code Game rul...
This article shares the specific code for JavaScr...
Table of contents Some basic instructions 1. Chec...
In fact, this is also a clickbait title, and it c...
I recently upgraded MySQL to 5.7, and WordPress r...
Preface Since vue3.0 was officially launched, man...
Table of contents 1. TypeScript is introduced int...