try...catch

来源:互联网 发布:ftp站点怎么绑定域名 编辑:程序博客网 时间:2024/05/16 12:36

try{} catch(e){}

一个花括号里应该是一个代码块,所以try和,catch后都有大括号.

try如果有异常,异常参数会给e.

比如

function add(a,b){
if(typeof a !=="number" || typeof b !=="number"){
throw{
name:'type error',
message:'add need number'
};
}
return a+b;
 }
 var try_it = function () {
try
 {add("a");} 
 catch(e){
document.write(e.name+':'+e.message);
}
}
try_it();

这里抛出的异常会在catch里处理.

暂时发现的异常包括在try里执行出现的异常,和try里执行的函数throw的异常.

原创粉丝点击