How does JavaScript handle exceptions? Try method for exception handling

青灯夜游
Release: 2018-11-10 11:56:24
Original
2768 people have browsed it

How does JavaScript handle exceptions? This article will give you a brief introduction to a method of handling exceptions in JavaScript: the try...catch...finally statement constructs the throw operator, so that you can understand how it handles exceptions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Let’s take a look at how the try...catch...finally statement block throw handles exceptions:

JavaScript can use try...catch...finally constructor and throw operator to handle exceptions.

This can catch program-generated and runtime exceptions, but you cannot catch JavaScript syntax errors.

1. try...catch...finally

Let's take a look at the try...catch...finally block Syntax:

Copy after login

The try block must be followed by a catch block or a finally block (or either). When an exception occurs in the try block, the exception is placed in 'e' and the catch block is executed. The optional finally block is executed unconditionally after the try/catch.

Example:

Example of calling a non-existent function, and this function raised an exception. Let's see how it behaves without try...catch

function myFunc() { var a = 100; alert("变量a的值为 : " + a); }
Copy after login

点击下面查看结果:

Copy after login

Run:

How does JavaScript handle exceptions? Try method for exception handling

Now let's try catching with try...catch This exception displays a user-friendly message. Users can also suppress this message if they want to hide this error.

function myFunc(){ var a = 100; ry { alert("变量a的值为 : " + a ); } catch ( e ) { alert("错误: " + e.description ); } }
Copy after login

Run:

How does JavaScript handle exceptions? Try method for exception handling

You can use finally block which will always execute unconditionally after try/catch. Here is an example:

function myFunc() { var a = 100; try { alert("变量a的值为 : " + a); } catch(e) { alert("错误: " + e.description); } finally { alert("Finally块将永远执行!"); } }
Copy after login

Run:

How does JavaScript handle exceptions? Try method for exception handling

## 2. throw

You can use the throw statement to raise built-in exceptions or custom exceptions, which can later be caught and appropriate actions can be taken.

Give an example: How to use throw statement

function myFunc() { var a = 100; var b = 0; try { if(b == 0) { throw("除以零时出现误差。"); } else { var c = a / b; } } catch(e) { alert("错误: " + e); } }
Copy after login
Run:


How does JavaScript handle exceptions? Try method for exception handling

You can use string, integer, Boolean The value or object throws an exception in a function and then catches the exception in the same function as above, or catches the exception in another function using a try...catch block.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of How does JavaScript handle exceptions? Try method for exception handling. 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!