[译]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 ...
随机推荐
- 【LeetCode】008. String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 如何重启mysql服务
window下: 在cmd中,键入services.msc,找到mysql,右键重启mysql
- bzoj 3629 [JLOI2014]聪明的燕姿——约数和定理+dfs
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3629 如果要搜索,肯定得质因数分解吧:就应该朝这个方向想. **约数和定理: 对于任意一个大 ...
- 用phpinfo( )打印出来的php版本和在服务器上用php -v打印出来的版本不同的原因
php -v 是linux系统的php版本,而phpinfo里显示的是WEB Server中配置的版本.说简单点,你的系统中有两个php版本. 如果您阅读过此文章有所收获,请为我顶一个,如果文章中有错 ...
- RazorHelper.cs
完整版 RazorHelper.cs using System; using System.Collections; using System.Collections.Generic; using S ...
- 蓝桥杯 算法训练 ALGO-150 6-1 递归求二项式系数值
算法训练 6-1 递归求二项式系数值 时间限制:10.0s 内存限制:256.0MB 问题描述 样例输入 一个满足题目要求的输入范例.3 10 样例输出 与上面的样例输入对应的输出. 数据 ...
- 蓝桥杯 算法训练 ALGO-124 数字三角形
算法训练 数字三角形 时间限制:1.0s 内存限制:256.0MB 问题描述 (图3.1-1)示出了一个数字三角形. 请编一个程序计算从顶至底的某处的一条路 径,使该路径所经过的数字的总和 ...
- boost::ASIO的同步方式和异步方式
http://blog.csdn.net/zhuky/article/details/5364574 http://blog.csdn.net/zhuky/article/details/536468 ...
- ZOJ 4016 Mergeable Stack(栈的数组实现)
Mergeable Stack Time Limit: 2 Seconds Memory Limit: 65536 KB Given initially empty stacks, the ...
- Oracle 下ASM磁盘总结
Oracle 下ASM磁盘总结 文章转载: Oracle下创建ASM磁盘总结https://blog.csdn.net/okhymok/article/details/78791841?utm_sou ...