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

Troubleshooting the reasons why MySQL deleted records do not take effect

A record of an online MySQL transaction problem L...

Vue implements sending emoticons in chat box

The specific code for sending emoticons in the vu...

Detailed explanation of the difference between Vue life cycle

Life cycle classification Each component of vue i...

How to create a virtual environment using virtualenv under Windows (two ways)

Operating system: windowns10_x64 Python version: ...

Implementation of waterfall layout in uni-app project

GitHub address, you can star it if you like it Pl...

MySQL 5.7.19 winx64 free installation version configuration tutorial

mysql-5.7.19-winx64 installation-free version con...

Solution to the problem of mysql service starting but not connecting

The mysql service is started, but the connection ...

Introduction to the use of this in HTML tags

For example: Copy code The code is as follows: <...

Windows 10 is too difficult to use. How to customize your Ubuntu?

Author | Editor Awen | Produced by Tu Min | CSDN ...

Introduction to HTML DOM_PowerNode Java Academy

What is DOM? With JavaScript, you can reconstruct...

How to set underline in HTML? How to underline text in HTML

Underlining in HTML used to be a matter of enclos...

Web Design: The Accurate Location and Use of Massive Materials

Three times of memorization allows you to remembe...

Native JS implements a very good-looking counter

Today I will share with you a good-looking counte...

How to use cc.follow for camera tracking in CocosCreator

Cocos Creator version: 2.3.4 Demo download: https...