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

Detailed explanation of try catch instances in javascript

零下一度
Release: 2017-04-18 11:03:24
Original
1658 people have browsed it

This article mainly introduces relevant information about the summary of try catch application in javascript. Friends who need it can refer to

Summary of try catch application in javascript

Example code:


<script language="Javascript"> 
try 
{ 
throw new Error(10,"asdasdasd") 
} 
catch (e) 
{ 
alert(e.message); 
alert(e.description) 
alert(e.number) 
alert(e.name) 
throw new Error(10,"asdasdasd") 
} 

</script>
Copy after login

You can use try...catch in Javascript for exception handling. For example:


try {
 foo.bar();
} catch (e) {
 alert(e.name + ": " + e.message);
}
Copy after login

Currently, the system exceptions we may get mainly include the following 6 types:

  1. ##EvalError : raised when an error occurs executing code in eval()

  2. RangeError: raised when a numeric variable or parameter is outside of its valid range

  3. ReferenceError: raised when de-referencing an invalid reference

  4. SyntaxError: raised when a syntax error occurs while parsing code in eval()

  5. TypeError: raised when a variable or parameter is not a valid type

  6. URIError: raised when encodeURI() or decodeURI() are passed invalid parameters

The above six exception objects all inherit from the Error object. They all support the following two construction methods:



new Error();
new Error("异常信息");
Copy after login

The method of manually throwing exceptions is as follows:



try {
 throw new Error("Whoops!");
} catch (e) {
 alert(e.name + ": " + e.message);
}
Copy after login

If you want to determine the type of exception information, you can do it in catch:


##

try {
 foo.bar();
} catch (e) {
 if (e instanceof EvalError) {
   alert(e.name + ":" + e.message);
 } 
 else if (e instanceof RangeError) {
   alert(e.name + ": " + e.message);
 } 
 // etc 
}
Copy after login

Error has the following main attributes:


    description: Error description (only available in IE).
  1. fileName: Error file name (only available in Mozilla).
  2. lineNumber: The number of lines with errors (only available in Mozilla).
  3. message: Error message (same description under IE)
  4. name: Error type.
  5. number: Error code (only available in IE).
  6. stack: Like Stack in Java Trace the same error stack information (only available to Mozilla).

  7. So in order to better understand the error message, we can change the catch part to the following form:


try {
 foo.bar();
} catch (e) {
 if (browserType != BROWSER_IE) {                
   alert("name: " + e.name + 
   "message: " + e.message + 
   "lineNumber: " + e.lineNumber + 
   "fileName: " + e.fileName + 
   "stack: " + e.stack);      
 } 
 else {            
   alert("name: " + e.name +    
   "errorNumber: " + (e.number & 0xFFFF ) + 
   "message: " + e.message");      
 } 
}
Copy after login

The throw command in Javascript can actually throw any object, and we can receive this object in catch. For example:


try {
 throw new Date(); // 抛出当前时间对象 
} catch (e) {
 alert(e.toLocaleString()); // 使用本地格式显示当前时间 
}
Copy after login

The above is the detailed content of Detailed explanation of try catch instances in javascript. 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
Popular Tutorials
More>
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!