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

Introduction to try catch usage in javascript

巴扎黑
Release: 2017-08-16 11:15:13
Original
1886 people have browsed it
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 six types:
EvalError: raised when an error occurs executing code in eval() 
RangeError: raised when a numeric variable or parameter is outside of its valid range 
ReferenceError: raised when de-referencing an invalid reference 
SyntaxError: raised when a syntax error occurs while parsing code in eval() 
TypeError: raised when a variable or parameter is not a valid type 
URIError: raised when encodeURI() or decodeURI() are passed invalid parameters
Copy after login

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 judge 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).
fileName: The file name of the error (only available in Mozilla).
lineNumber: The number of lines in error (only available in Mozilla).
message: Error message (same as description under IE)
name: Error type.
number: Error code (only available in IE).
stack: Error stack information like Stack Trace in Java (only available for Mozilla).
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 Introduction to try catch usage 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