• JS中常见的几种报错类型


    1、JS中常见的几种错误

    1.1、SyntaxError(语法错误)

    解析代码时发生的语法错误

    var 1a;   //Uncaught SyntaxError: Invalid or unexpected token 变量名错误 
    console.log 'hello');  //Uncaught SyntaxError: Unexpected string 缺少括号

    1.2、ReferenceError(引用错误)

    console.log(a);  //Uncaught ReferenceError: a is not defined 引用了一个不存在的变量
    console.log()= 1;  //Uncaught ReferenceError: Invalid left-hand side in assignment 将变量赋值给一个无法被赋值的对象

    1.3、RangeError(范围错误)

    var a = new Array(-1);  //Uncaught RangeError: Invalid array length 超出有效范围

    1.4、TypeError(类型错误)

    变量或参数不是预期类型,比如,对字符串、布尔值、数值等原始类型的值使用new命令,就会抛出这种错误,因为new命令的参数应该是一个构造函数。

    var a = new 123;  //Uncaught TypeError: 123 is not a function

    调用对象不存在的方法

    var a;
    a.aa(); //Uncaught TypeError: Cannot read property 'aa' of undefined

    1.5、URLError(URL错误)

    与url相关函数参数不正确,主要是encodeURI()、decodeURI()、encodeURIComponent()、decodeURIComponent()、escape()和unescape()这六个函数。

    decodeURI('%2')  //Uncaught URIError: URI malformed

    1.6、EvalError(eval错误)

    eval函数没有被正确执行

    2、手动抛出错误

    以上这6种派生错误,连同原始的Error对象,都是构造函数。开发者可以使用它们,人为生成错误对象的实例。

    throw new Error("出错了!"); 
    throw new RangeError("出错了,变量超出有效范围!"); 
    throw new TypeError("出错了,变量类型无效!");

    上面代码表示新建错误对象的实例,实质就是手动抛出错误。可以看到,错误对象的构造函数接受一个参数,代表错误提示信息(message)。

  • 相关阅读:
    beautiful number 数位DP codeforces 55D
    最长上升子序列
    0-1背包 codeforces 55 D
    概率DP HDU 4586 play the dice
    水题 不要62 HDU 2089
    抓老鼠 codeForce 148D
    ZOJ 3551 吸血鬼 概率DP
    poj 2151 Check the difficulty of problems 概率DP
    HDU 4681 string 求最长公共子序列的简单DP+暴力枚举
    HDU 1814 模板题 2-sat
  • 原文地址:https://www.cnblogs.com/wenxuehai/p/10319204.html
Copyright © 2020-2023  润新知