JavaScript error handling try..catch...finally + covers throw+TypeError+RangeError

JavaScript error handling try..catch...finally + covers throw+TypeError+RangeError

1. Purpose

Normally, 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. Grammar

try {
  // 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

Catch catches all error .

If we don't know how to handle it, we throw err .

The throw operator generates an error object.

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: TypeError , RangeError , SyntaxError

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:

  • TypeError : (Type Error) The object used to represent an error that occurs when the type of a value is not the expected type
  • RangeError : The object indicates an error when a value is not in its allowed range or set.
  • SyntaxError : When the Javascript language parses the code, the Javascript engine finds tokens or token sequences that do not conform to the grammatical specifications and throws

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:
  • Detailed explanation of common JS errors and solutions
  • javascript Error object error handling

<<:  What is ssh port forwarding? What's the use?

>>:  CSS realizes div completely centered without setting height

Recommend

Detailed explanation of nginx configuration file interpretation

The nginx configuration file is mainly divided in...

Several reasons for not compressing HTML

The reason is simple: In HTML documents, multiple ...

Full process record of Nginx reverse proxy configuration

1. Preparation Install Tomcat on Linux system, us...

js implements the pop-up login box by clicking the pop-up window

This article shares the specific code of js to re...

Is the tag li a block-level element?

Why can it set the height, but unlike elements lik...

JS realizes the automatic playback effect of pictures

This article shares the specific code of JS to ac...

Mobile web screen adaptation (rem)

Preface I recently sorted out my previous notes o...

How to use React to implement image recognition app

Let me show you the effect picture first. Persona...

Application and implementation of data cache mechanism for small programs

Mini Program Data Cache Related Knowledge Data ca...

Example code for implementing hexagonal borders with CSS3

The outermost boxF rotates 120 degrees, the secon...

Detailed explanation of react setState

Table of contents Is setState synchronous or asyn...

Introduction to using MySQL commands to create, delete, and query indexes

MySQL database tables can create, view, rebuild a...