[译]Javascript中的错误信息处理(Error handling)
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单
源地址在此:
https://www.youtube.com/watch?v=PMsVM7rjupU&list=PL6n9fhu94yhUA99nOsJkKXBqokT3MBK0b
在Javascript中使用try/catch/finally来处理runtime的错误.这些runtime错误被称为exceptions.各种各样的原因都可能导致exception.比如,使用没有申明的变量或者方法都可能导致exception.
可能导致exceptions的Javascript语句都应该被包裹在try语句内.当try语句内的某一句导致exception的时候,控制权就立刻转移到catch语句内,然后跳过try语句内的剩下所有代码内容.
Javascript try catch例子:
try
{
// Referencing a function that does not exist cause an exception
// 使用不存在的函数导致产生一个exception
document.write(sayHello());
// Since the above line causes an exception, the following line will not be executed
// 因为以上的代码导致出现exception,所以这以下的代码将不会运行
document.write("This line will not be executed");
}
// When an exception occurs, the control is transferred to the catch block
//当exception产生后,控制权就转交到了catch语句块中 catch (e)
{
document.write("Description = " + e.description + "[br/]");
document.write("Message = " + e.message + "[br/]");
document.write("Stack = " + e.stack + "[br/][br/]");
}
document.write("This line will be executed");
Output : // JavaScript try catch example.png
请注意:一个try语句块应该始终紧跟一个catch语句块或者一个finally语句块,或者两者兼有
finally语句块无论exception是否产生都会运行.其主要用处在于清理和释放脚本运行期间所占用的资源.举个例子,如果你的try语句块打开了一个文件且运行,而好死不死又发生了一个exception,那么finally语句块就是用来关闭文件的.
Javascript try catch finally例子:
try
{
// Referencing a function that does not exist cause an exception
document.write(sayHello());
// Since the above line causes an exception, the following line will not be executed
document.write("This line will not be executed");
}
// When an exception occurs, the control is transferred to the catch block
catch (e)
{
document.write("Description = " + e.description + "[br/]");
document.write("Message = " + e.message + "[br/]");
document.write("Stack = " + e.stack + "[br/][br/]");
}
finally
{
document.write("This line is guaranteed to execute");
}
Output : JavaScript try catch finally example.png
Javascript中的格式错误与exceptions需要注意的是:
try/catch/finally语句块可以捕捉到exceptions,但是格式错误则无法被其捕捉到.
例子:以下代码中有一个格式错误-丢失了闭小括号.catch语句块则无法捕捉到这个错误
try
{
alert("Hello";
}
catch (e)
{
document.write("JavaScript syntax errors cannot be caught in the catch block");
}
Javascript throw语句:用throw语句来捕捉自定义exceptions
Javascript throw exception例子:
JavaScript throw exception example :
function divide()
{
var numerator = Number(prompt("Enter numerator"));
var denominator = Number(prompt("Enter denominator")); try
{
if (denominator == 0)
{
throw {
error: "Divide by zero error",
message: "Denominator cannot be zero"
};
}
else
{
alert("Result = " + (numerator / denominator));
} }
catch (e)
{
document.write(e.error + "[br/]");
document.write(e.message + "[br/]");
}
} divide();
[译]Javascript中的错误信息处理(Error handling)的更多相关文章
- javascript中的错误处理机制
× 目录 [1]对象 [2]类型 [3]事件[4]throw[5]try[6]常见错误 前面的话 错误处理对于web应用程序开发至关重要,不能提前预测到可能发生的错误,不能提前采取恢复策略,可能导致较 ...
- [译]Javascript中的本地以及全局变量
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- [译]JavaScript中,{}+{}等于多少?
最近,Gary Bernhardt在一个简短的演讲视频“Wat”中指出了一个有趣的JavaScript怪癖:在把对象和数组混合相加时,会得到一些你意想不到的结果.本篇文章会依次讲解这些计算结果是如何得 ...
- [译]Javascript中的闭包(closures)
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- [译]Javascript中的数列
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- [译]Javascript中的for循环
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- [译]Javascript中的do-while循环
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- [译]Javascript中的循环
本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...
- MySQL Cluster测试过程中的错误汇总--ERROR 1296 (HY000)等等
参考资料: http://dev.mysql.com/doc/refman/5.1/en/mysql-cluster-privilege-distribution.html http://www.cl ...
随机推荐
- CODE FESTIVAL 2017 qual A--B-fLIP(换种想法,暴力枚举)
个人心得:开始拿着题目还是有点懵逼的,以前做过相同的,不过那是按一个位置行列全都反之,当时也是没有深究.现在在打比赛不得不 重新构思,后面一想把所有的状态都找出来,因为每次确定了已经按下的行和列后,按 ...
- redis设置为null问题
查看源码后发现,redis没有删除方法,本想给他设置为null,但是redis报错,所有仔细想了一下,发现redis提供了一个时间限制方法,所有可以让redis的时间限制为1s,就想当于删除redis ...
- BZOJ1202:[HNOI2005]狡猾的商人
浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...
- web打印详解
在B/S模式开发中,打印是个很大的困扰.无论是采用页面直接输出或者引用WORD.DLL也好,都有不足之处. 目前最好的办法就是采用第三方控件,网上流传的打印控件有很多.总结了下推荐几个给大家: 一.首 ...
- Linux内核 - 定时器
#include <linux/timer.h> //头文件 struct timer_list mytimer; //定义变量 static void my_timer(unsigned ...
- C# winfrom FastReport Print
1.引用 using FastReport; using FastReport.Barcode; 2.code private void toolStripButtonPrint_Click(obje ...
- Oracle 数据库加密
数据加密 动态数据(data in motion)和静态数据(data at rest),除了手动加密,其他的加密都需要oracle企业版的高级加密(额外收费——) 1 静态数据加密 Example ...
- volatile语义
volatile在Java内存模型(JMM)中,保证共享变量对所有线程可见,但不保证原子性.volatile语义是同步,通过共享变量的方式,完成线程间的通信. 为什么需要volatile Java内存 ...
- 读取XML文件的指定节点的值 并转换为Item
cmb_State_Send.ItemsSource = null; XmlDocument doc = new XmlDocument(); doc.Load("D:\\模板\\Works ...
- C#一般处理程序 ashx.cs使用Session报错问题
HttpContext.Current.Session["UserID"].ToString();//报错,报Session为Null, 此时需要添加引用和继承IRequiresS ...