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

Introduction to MySQL <> and <=> operators

<> Operator Function: Indicates not equal t...

Example of horizontal arrangement of li tags in HTMl

Most navigation bars are arranged horizontally as...

Summary of Several Methods for Implementing Vertical Centering with CSS

In the front-end layout process, it is relatively...

How to install redis5.0.3 in docker

1. Pull the official 5.0.3 image [root@localhost ...

Achieve 3D flip effect with pure CSS3 in a few simple steps

As a required course for front-end developers, CS...

Vue Getting Started with Weather Forecast

This article example shares the specific code of ...

Detailed explanation of Vue slot

1. Function : Allows the parent component to inse...

Docker Data Storage Volumes Detailed Explanation

By default, the reading and writing of container ...

The HTML 5 draft did not become a formal standard

<br />Yesterday I saw at W3C that the new HT...

Windows Server 2008 R2 Multi-User Remote Desktop Connection Licensing

At work, we often need remote servers and often e...