Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Which statement is used by javascript to catch exceptions

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you which sentence is used to catch exceptions in javascript, I believe most people do not know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it together.

Javascript catches an exception with a "try catch" statement, syntax "try {/ / code where an exception may occur} catch (error) {/ / actions to be performed when an exception occurs"; the error code is captured in the try statement block, and the method of handling the exception is defined in the catch statement block.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

JS exception handling

The purpose of exception handling is to catch the code that generated the exception, so that the whole program will not stop running because of the exception. In JavaScript, you can use try catch statements to catch exceptions and handle them accordingly. The syntax format is as follows:

Try {/ / Code where an exception may occur} catch (error) {/ / actions to be performed when an exception occurs}

We can put any code that may cause an exception into a try statement block and define a method to handle the exception in the catch statement block. If an error occurs in the code in the try statement block, the code immediately jumps from the try statement block to the catch statement block, and if there is no error in the code in the try statement block, the code in the catch statement block is ignored.

Try {var title = "JavaScript"; [xss_clean] (title); / / call an undefined variable [xss_clean] (str); / / if an error occurs, the following line alert ("all statements have been executed successfully.") ;} catch (error) {/ / handle error alert ("error message:" + error.message);} / / continue to execute the following code [xss_clean] ("

Hello World!

")

The running result is shown in the following figure:

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

Name: type of error

Message: a description of the error.

Try catch finally statement

After the try catch statement, you can also add a block of finally statements, and the code in the try statement executes regardless of whether the code in the finally statement block has an error or not. The sample code is as follows:

/ / receive the user input parameter var num = prompt ("enter a number from 0 to 100"); / / get the current time var start = Date.now (); try {if (num > 0 & & num)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report