zoukankan      html  css  js  c++  java
  • 捕获程序异常之tryCatch

    一、try catch语法
    try…catch…finally…语法中除了try以外,catch和finally都是可选的(两者必须要有一个),也就是说try…catch…finally…语法有以下三种形式。

    形式1

    try{
    //do something
    }
    catch(e){
    //somecode
    }
    finally{
    //do something
    }

    形式2

    try{
    //do something
    }
    catch(e){
    //somecode
    }

    形式3

    try{
    //do something
    }
    finally{
    //do something
    }

    二、try catch用法
    避免在循环中使用try-catch-finally

    var $myObj = ['foo', 'bar'], i;  
    //不好的用法
    for (i = 0, len = $myObj.length; i <len; i++) {  
        try {  
            // do something that throws an exception 
        }  
        catch (e) {   
            // handle exception  
        } 
    }
    //好的用法
    try { 
        for (i = 0, len = $myObj.length; i <len; i++) {  
            // do something that throws an exception 
        } 
    } 
    catch (e) {   
       window.location.href = "http://stackoverflow.com/search?q=[js]+" + e.message;
    } 
  • 相关阅读:
    GolandQuick编辑器快捷键
    GitStand
    高阶函数
    文本和字节序列
    元组用法
    映射的弹性键查询
    字典的setdefault()
    数组、内存视图、双向队列
    Python之random.seed()用法
    用bisect来管理已排序的序列
  • 原文地址:https://www.cnblogs.com/camille666/p/exception_trycatch.html
Copyright © 2011-2022 走看看