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
This article introduces blue-green deployment and...
This article shares the specific code of Vue to a...
Element form and code display For details, please...
This article describes the MySQL data types and f...
Table of contents 1. Source code 1.1 Monorepo 1.2...
Simply put, delayed replication is to set a fixed...
This article records the installation graphic tut...
1. Log in to mysql: mysql -u root -h 127.0.0.1 -p...
In the project, it is necessary to obtain the lat...
This article shares the specific code of js to im...
question Question 1: How to solve the performance...
When I was writing the login page today, I needed...
Table of contents 1. Understanding Databases 1.1 ...
1. Install JDK 1. Uninstall the old version or th...
<br />In HTML language, you can automaticall...