What statement does JavaScript use to catch exceptions?

青灯夜游
Release: 2021-10-21 09:16:43
Original
3628 people have browsed it

Javascript uses the "try catch" statement to catch exceptions. The syntax is "try {// Code where exceptions may occur} catch(error) {// Operations to be performed when an exception occurs}"; in the try statement block Capture error codes, and define methods for handling exceptions in the catch statement block.

What statement does JavaScript use to catch exceptions?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

JS Exception Handling

The purpose of exception handling is to capture the code that generates exceptions so that the entire program will not terminate due to exceptions. In JavaScript, you can use thetry catchstatement to catch exceptions and handle them accordingly. The syntax format is as follows:

try { // 可能会发生异常的代码 } catch(error) { // 发生异常时要执行的操作 }
Copy after login

We can put any code that may cause exceptions into the try statement block, and define methods for handling exceptions in the catch statement block. If an error occurs in the code in the try statement block, the code will immediately jump from the try statement block to the catch statement block. If no error occurs in the code in the try statement block, the code in the catch statement block will be ignored.

Copy after login

The running results are as shown below:

What statement does JavaScript use to catch exceptions?

When an exception occurs in the code in the try statement block, an Error object will be created and thrown (for example The error in catch(error) in the above code), the object contains two attributes, as shown below:

  • name: the type of error;

  • message: Description of the error.

try catch finallyStatement

After the try catch statement, you can also add A finally statement block, regardless of whether an error occurs in the code in the try statement block, the code in the finally statement will be executed. The sample code is as follows:

Copy after login

If we enter a number less than 100, such as 88, the running results are as follows:

1.3015928349429721e+171 代码执行花费了:0ms
Copy after login

If we enter a number greater than 100, such as 123, the running results are as follows:

输入的值无效! 代码执行花费了:0ms
Copy after login

[Recommended learning:javascript advanced tutorial]

The above is the detailed content of What statement does JavaScript use to catch exceptions?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!