Home >Web Front-end >JS Tutorial >Detailed explanation of try catch instances in javascript

Detailed explanation of try catch instances in javascript

零下一度
零下一度Original
2017-04-18 11:03:241703browse

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>

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


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

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("异常信息");

The method of manually throwing exceptions is as follows:



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

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 
}

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");      
 } 
}

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()); // 使用本地格式显示当前时间 
}

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!

Statement:
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