zoukankan      html  css  js  c++  java
  • 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语句,那么即使已经抛出了一个异常,而且该异常还没有被处理,该方法也会正常返回。 

  • 相关阅读:
    《Android深度探索HAL与驱动开发》第一章阅读心得
    《Android深度探索HAL与驱动开发》第二章阅读心得
    《Android深度探索HAL与驱动开发》第三章阅读心得
    第五章 搭建S3C6410开发板测试环境
    IOS 拾取器控件
    原生js实现jquery的getJSON方法
    css reset
    css3特效
    HTML5 有哪些特质
    css rest
  • 原文地址:https://www.cnblogs.com/lindsayzhao103011/p/3190498.html
Copyright © 2011-2022 走看看