Javascript之错误处理篇

//noneXistentFunction 引发错误处理机制   
try{   
    window.noneXistentFunction();   
    alert("Method completed");   
} catch (exception) {   
//因为Javascript是弱类型语言,   
//所以只能有一个catch语句,   
//并不能分类捕获Exception,   
//所有的Exception在一个catch中处理   
    alert("an exception occurred" + exception.message);   
} finally {   
    alert("End of try ... catch test");   
}   
  
//嵌套 TRY CATCH   
  
try {   
    eval("a++b");   
} catch (exception) {   
    alert("an exception occurrd " + exception.message);   
    try {   
        var aErrors = new Array(100000000000000000000000000000000000000);   
    } catch (exception){   
        alert("another exception occurred");   
    }   
} finally {   
    alert("all done");   
}   
  
//判断错误类型   
try {   
    eval("a++b");   
} catch (exception) {   
//通过NAME 判断   
    if(exception.name == "SyntaxError") {   
        alert("Syntax Error " + exception.message);   
    } else {   
        alert("An Unexpected Error Occurred " + exception.message);   
    }   
       
    try {   
        var aErrors = new Array(100000000000000000000000000000000000000);   
    } catch (exception){   
    //通过 INSTANCEOF 判断   
        if(exception instanceof SyntaxError) {   
            alert("Syntax Error " + exception.message);   
        } else if (exception instanceof EvalError){   
            alert("Eval Function Error " + exception.message);   
        } else if (exception instanceof ReferenceError){   
            alert("Reference Error " + exception.message);   
        } else if (exception instanceof RangeError){   
            alert("Number Range Error " + exception.message);   
        } else if (exception instanceof TypeError){   
            alert("variable Type Error " + exception.message);   
        } else if (exception instanceof URIError){   
            alert("encodeURI or decodeURI Function Error " + exception.message);   
        } else {   
            alert("An Unexpected Error Occurred " + exception.message);   
        }   
    }   
} finally {   
    alert("all done");   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值