Home >Web Front-end >JS Tutorial >Detailed explanation of try catch instances in javascript
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:
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 }
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"); } }
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!