functionfoo(num){// 检查传入的参数是否为数值if(typeof num !=="Number"){throw{
type:"Value error",
info:"The value is not right, it should be a number!"};}else{return num;}}functiontestMethod(){try{foo("fine");//=> 传入一个非数字字面量,使函数触发异常}catch(e){
console.log(e.type +":"+ e.info);returnfalse//=> 并不会作为返回值
console.log("here")//=> 这里的代码不会执行}finally{returntrue;//=> 作为最终返回值}}
console.log(testMethod());//=> truefunctiontestMethodTwo(){try{foo("hello")//=> 触发函数的异常}catch(e){
console.log("here is catch clause.");throw e;//=> 被finally覆盖,且这里这条语句会被挂起,直到finally子句执行完毕}finally{returntrue;//=> 函数最终依然返回true值}}
console.log(testMethodTwo());//=> 调用函数
console.log("here is ending!");//=> 这里的语句依然能正常被执行