• js Exception


    1. js的异常e除了e.message 和 e.lineNumber之外还有一个重要的属性 e.name 
      e.name 错误类型, 
      e.message 错误的详细信息. 
      Error.name的六种值对应的信息: 
        1. EvalError:eval()的使用与定义不一致 
        2. RangeError:数值越界 
        3. ReferenceError:非法或不能识别的引用数值 
        4. SyntaxError:发生语法解析错误 
        5. TypeError:操作数类型错误 
        6. URIError:URI处理函数使用不当 

    2. Error具有下面一些主要属性:

    • description: 错误描述 (仅IE可用).
    • fileName: 出错的文件名 (仅Mozilla可用).
    • lineNumber: 出错的行数 (仅Mozilla可用).
    • message: 错误信息 (在IE下同description)
    • name: 错误类型.
    • number: 错误代码 (仅IE可用).
    • stack: 像Java中的Stack Trace一样的错误堆栈信息 (仅Mozilla可用).

      因此为了更好的了解错误信息我们可以将catch部分改为如下形式:Js代码    

        try { 
          fo.bar(); 
        } catch (e) { 
          if (browserType != BROWSER_IE) { 
          alert("name: " + e.name + 
          "message: " + e.message + 
          "lineNumber: " + e.lineNumber + 
          "fileName: " + e.fileName + 
          "stack: " + e.stack); 
        } else { 
          alert("name: " + e.name + 
          "errorNumber: " + (e.number & 0xFFFF ) + 
          "message: " + e.message"); 
        }

    3. JavaScript中的throw命令事实上可以抛出任何对象,并且我们可以在catch接受到此对象。例如: 

      try { 
        throw new Date(); // 抛出当前时间对象 
      } catch (e) { 
        alert(e.toLocaleString()); // 使用本地格式显示当前时间 
      }

    4. try catch finally

      try {
        // 通常,代码会从顶部运行到底部
        // 可以直接用throw语句或通过调用一个抛出异常的方法间接的 抛出异常
      } catch (e) {
        // The statements in this block are executed if, and only if, the try block throws an exception. These statements can use the local variable
        // e to refer to the Error object or other value that was thrown.
        // This block may handle the exception somehow, may ignore the
        // exception by doing nothing, or may rethrow the exception with throw.
      } finally {
        // This block contains statements that are always executed, regardless of
        // what happens in the try block. They are executed whether the try
        // block terminates:
          // 1) normally, after reaching the bottom of the block
          // 2) because of a break, continue, or return statement
          // 3) with an exception that is handled by a catch clause above
          // 4) with an uncaught exception that is still propagating
      }

      如果finally块自身用return语句、continue语句、break语句或throw语句转移了控制流、或者调用了抛出异常的方法改变了控制流,
      那么等待的控制流转移将被舍弃,并进行了新的转移,例如:若finally从句抛出了一个异常,那么该异常将代替处于抛出过程中的异常。
      如果finally从句用到了return语句,那么即使已经抛出了一个异常,而且该异常还没有被处理,该方法也会正常返回。 

  • 相关阅读:
    apt-get connects to web
    Particle Filter(CC) 放狗
    Get a specific pixel coordinates where your mouse on (cc)
    event
    两张图片的平移实验 (SLAM translate epipolar geometry)
    vs2010 LINK : fatal error LNK1123: 转换到 COFF 期间失败:(cc)
    fx and fy in Calibration
    How do I get add-apt-repository to work through a proxy?
    ROS and PCL install
    Longest palindrome subsequence
  • 原文地址:https://www.cnblogs.com/lindsayzhao103011/p/3190498.html
Copyright © 2020-2023  润新知