Home > Web Front-end > JS Tutorial > body text

Summary of exception handling examples in javascript programming_javascript skills

WBOY
Release: 2016-05-16 15:28:52
Original
1229 people have browsed it

The examples in this article summarize the methods of exception handling in JavaScript programming. Share it with everyone for your reference, the details are as follows:

Foreword: In the previous article "Summary of common public exception catching methods in asp.net development", we summarized the exception handling on the asp.net server. This article follows the previous article and briefly summarizes and discusses JavaScript exception handling on the client side. In this way, we have a preliminary understanding of asp.net server-side and client-side exception handling.

1. Annoying script errors

Lou Zhu often plays 13, but generally there is no depth. Occasionally, I struggled to understand a passage in English, and finally I could pretend again in depth:

When browsing Web pages on the internet, we all have seen a JavaScript alert box telling us there is a runtime error and asking "Do you wish to debug?". Error message like this may be useful for developers but not for users . When users see errors, they often leave the Web page.

Hey, don’t you understand the above paragraph? To put it elegantly and crudely, nclouzhu understands that when opening a web page, we have all encountered SB problems such as a script error popping up on the web page and asking "Do you want to debug?" from time to time. Isn’t it annoying? Normal users often habitually select the red cross in the upper right corner, but this kind of prompt information may be very useful to developers. It can be seen that, I kao, the developers are not normal? ! It seems that Lou Zhu misunderstood. In fact, it is not difficult to see that the ultimate intention of the original text should be to tell us that script errors in web pages are very serious and the user experience is not good, which will "scare away" a group of potential users in vain.

2. How to deal with script errors

In js, we usually catch and handle exceptions through try...catch.

try
{
 //Run some code here
}
catch(e)
{
 //Handle errors here
}

Copy after login

In actual code, we might write:

function test(){
var txt="";
try{
  alert(aaa);//aaa是未声明的变量
}
catch(e){
  txt="There was an error on this page.\n\n";
  txt+="Error message: " + e.message + "\n\n";
  txt+="Error description: " + e.description + "\n\n";
  txt+="Error name: " + e.name + "\n\n";
  //alert(txt);//正式平台上可能需要注释掉该行
  }
}

Copy after login

Another common approach is to register a general processing method for the onerror event of the window object and place the following code in the section of the page:

window.onerror=function(){
  return true;
}

Copy after login

The advantage of the above method is that if you write it once on the page, annoying script errors will not pop up. It is a bit global processing. For developers, this way of writing may hide potential script errors from being discovered, so the above functions need to be commented out when testing.

3. Error in javascript

(1), Common attributes of Error object

When we catch an exception, we usually throw an instance e of the Error object at the catch. Several common properties of e are as follows:

Attribute Description
description Exception description information
Message abnormal description information
name Exception type
number Unique exception code

In actual development, developers are usually prompted with message and name information in order to handle exceptions in a targeted manner.

(2), Type of Error object

We can check the exception type through the name attribute in (1). In js, there are several common exception types as follows:

TypeError : Raised when an unexpected type is encountered, such as undeclared variables;
SyntaxError : Triggered by syntax errors when parsing js code, such as server-side registration scripts, missing brackets or quotation marks, etc.;
ReferenceError: This exception is raised when an invalid reference is used;
EvalError : Raised when the eval function is called incorrectly;
RangeError : Raised when the value of a numeric variable exceeds its range;
URIError : Raised when the encodeURI() or decodeURI() function is used incorrectly.

In actual development, different exception handling for different types of exceptions will help us effectively discover problems and improve user experience.

I hope this article will be helpful to everyone in JavaScript programming.

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template