console.log(t);//Uncaught ReferenceError: t is not defined
//一般地,使用try-catch语句来捕获错误
try{
t;
}catch(ex){
console.log(ex.message);//t is not defined
console.log(ex.name);//ReferenceError
}
try{
t;
}catch(ex){
console.log(ex.stack);//@file:///D:/wamp/www/form.html:12:2
}
new Error();
new Error(message);
//一般地,使用throw语句来抛出错误
throw new Error('test');
//Uncaught Error: test
throw new Error();//Uncaught Error
function UserError(message) {
this.message = message;
this.name = "UserError";
}
UserError.prototype = new Error();
UserError.prototype.constructor = UserError;
throw new UserError("errorMessage");//Uncaught UserError: errorMessage
Error();
Error(message);
throw Error('test');//Uncaught Error: test
throw Error();//Uncaught Error
var test = new Error('testError');
console.log(test.toString());//'Error: testError'
Error EvalError(eval错误) RangeError(范围错误) ReferenceError(引用错误) SyntaxError(语法错误) TypeError(类型错误) URIError(URI错误)
new Array(-1);//Uncaught RangeError: Invalid array length new Array(Number.MAX_VALUE);//Uncaught RangeError: Invalid array length (1234).toExponential(21);//Uncaught RangeError: toExponential() argument must be between 0 and 20 (1234).toExponential(-1);////Uncaught RangeError: toExponential() argument must be between 0 and 20
a;//Uncaught ReferenceError: a is not defined 1++;//Uncaught ReferenceError: Invalid left-hand side expression in postfix operation
//变量名错误 var 1a;//Uncaught SyntaxError: Unexpected number // 缺少括号 console.log 'hello');//Uncaught SyntaxError: Unexpected string
var o = new 10;//Uncaught TypeError: 10 is not a constructor
alert('name' in true);//Uncaught TypeError: Cannot use 'in' operator to search for 'name' in true
Function.prototype.toString.call('name');//Uncaught TypeError: Function.prototype.toString is not generic
decodeURI('%2');// URIError: URI malformed
//DOM0级
window.onerror = function(message,url,line){
alert(message);
}
//DOM2级
window.addEventListener("error",function(message,url,line){
alert(message);
});
//控制台显示错误消息
window.onerror = function(message,url,line){
alert(message);
return false;
}
a;
//控制台不显示错误消息
window.onerror = function(message,url,line){
alert(message);
return true;
}
a;
var image = new Image();
image.src = 'smilex.gif';
image.onerror = function(e){
console.log(e);
}
throw 12345;
throw 'hello world';
throw true;
throw {name: 'javascript'};
throw new Error('something bad happened');
throw new SyntaxError('I don\'t like your syntax.');
throw new TypeError('what type of variable do you take me for?');
throw new RangeError('sorry,you just don\'t have the range.');
throw new EvalError('That doesn\'t evaluate.');
throw new URIError('URI, is that you?');
throw new ReferenceError('you didn\'t cite your references properly');
function CustomError(message){
this.name = 'CustomError';
this.message = message;
}
CustomError.prototype = new Error();
throw new CustomError('my message');
try{
//通常来讲,这里的代码会从头到尾而不会产生任何问题
//但有时会抛出一个异常,要么是由throw语句直接抛出,要么通过调用一个方法间接抛出
}catch(e){
//当且仅当try语句块抛出了异常,才会执行这里的代码
//这里可以通过局部变量e来获得对Error对象或者抛出的其他值的引用
//这里的代码块可以基于某种原因处理这个异常,也可以忽略这个异常,还可以通过throw语句重新抛出异常
}finally{
//不管try语句是否抛出了异常,finally里的逻辑总是会执行,终止try语句块的方式有:
//1、正常终止,执行完语句块的最后一条语句
//2、通过break、continue或return语句终止
//3、抛出一个异常,异常被catch从句捕获
//4、抛出一个异常,异常未被捕获,继续向上传播
}
try{
q;
}catch(error){
alert(error.message);//q is not defined
}
//Uncaught SyntaxError: Unexpected token )
try{
q;
}catch(){
alert(error.message);
}
function throwIt(exception) {
try {
throw exception;
} catch (e) {
console.log('Caught: '+ e);
}
}
throwIt(3);// Caught: 3
throwIt('hello');// Caught: hello
throwIt(new Error('An error happened'));// Caught: Error: An error happened
try{
throw "出错了";
} catch (e) {
console.log(111);
}
console.log(222);
// 111
// 222
try {
foo.bar();
} catch (e) {
if (e instanceof EvalError) {
console.log(e.name + ": " + e.message);
} else if (e instanceof RangeError) {
console.log(e.name + ": " + e.message);
}
// ...
}
//由于没有catch语句块,所以错误没有捕获。执行finally代码块以后,程序就中断在错误抛出的地方
function cleansUp() {
try {
throw new Error('出错了……');
console.log('此行不会执行');
} finally {
console.log('完成清理工作');
}
}
cleansUp();
// 完成清理工作
// Error: 出错了……
function testFinnally(){
try{
return 2;
}catch(error){
return 1;
}finally{
return 0;
}
}
testFinnally();//0
var count = 0;
function countUp() {
try {
return count;
} finally {
count++;
}
}
countUp();// 0
console.log(count);// 1
function f() {
try {
console.log(0);
throw "bug";
} catch(e) {
console.log(1);
return true; // 这句原本会延迟到finally代码块结束再执行
console.log(2); // 不会运行
} finally {
console.log(3);
return false; // 这句会覆盖掉前面那句return
console.log(4); // 不会运行
}
console.log(5); // 不会运行
}
var result = f();
// 0
// 1
// 3
console.log(result);// false
try{
throw new Error();//抛出错误
}catch(e){
console.log(e);//Error(…)
}
console.log(e);//Uncaught ReferenceError: e is not defined
function concat(str1,str2,str3){
var result = str1 + str2;
if(str3){ //绝对不要这样
result += str3;
}
return result;
}
function concat(str1,str2,str3){
var result = str1 + str2;
if(typeof str3 == 'string'){ //更合适
result += str3;
}
return result;
}
//不安全的函数,任何非数组值都会导致错误
function reverseSort(values){
if(values){
values.sort();
values.reverse();
}
}
//不安全的函数,任何非数组值都会导致错误
function reverseSort(values){
if(values != null){
values.sort();
values.reverse();
}
}
//不安全的函数,任何非数组值都会导致错误
function reverseSort(values){
if(typeof values.sort == 'function'){
values.sort();
values.reverse();
}
}
//安全,非数组值被忽略
function reverseSort(values){
if(values instanceof Array){
values.sort();
values.reverse();
}
}
//错误 http://www.yourdomain.com/?redir=http://www.sometherdomain.com?a=b&c=d //针对'redir='后面的所有字符串调用encodeURIComponent()就可以解决这个问题 http://www.yourdomain.com/?redir=http:%3A%2F%2Fwww.sometherdomain.com%3Fa%3Db%26c%3Dd
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有