javascript The Definitive Guide Chapter 17 error exception handling

function TestTryCatch(){
    try {
        
    } catch (error) {
        //error 类型如下 Error EvalError RangeError ReferenceError
        //SyntaxError TypeError  URIError
        if(error instanceof TypeError){
            
        }else if( error instanceof ReferenceError){

        }else{

        }
    }
    finally{

    }
}

//17.2.2 抛出错误

function process(values){
    if(!(values instanceof Array)){
        throw new Error('process():Argument must be an array');
    }
    values.sort();

    for(var i=0,len=values.length;i<len;i++){
        if(values[i] >100){
            return values[i];
        }
    }

    return -1;

}

//自定义错误
function CustomError(message)
{
   this.name ='CustomError';
   this.message = message;
}
CustomError.prototype =new Error();


//17.2.3 错误(error)事件
window.onerror =function(message,url,line){
    alert(message);
    return false;
}

//throw new CustomError('CustomError');

var image = new Image();
image.addEventListener('load',function(event){
    alert('Image loaded');
},false);
image.addEventListener('error',function(event){
    alert('Image not laoded');
},false);
image.src ='smilex.gif';


//17.2.4 error handling strategy
// slightly 

//17.2.5 common type of error 
function the concat (str1, str2, Str3) { 
    var = Result + str1 str2; 
    IF (typeof Str3 == 'String') { 
        Result + = Str3; 
    } 
    return Result; 
} 
the getQueryString function (URL) { 
    IF (URL == typeof 'String') { 
        var url.indexOf POS = ( '?'); 
        IF (POS> -1) { 
            return url.substring (+ POS. 1); 
        } 
    } 
} 
reverseSort function (values) { 
    IF (typeof values.sort == 'function') { 
        values.sort (); 
        values.reverse (); 
    } 
} 


//17.2.6 distinguish fatal errors and non-fatal errors 


//17.2.7 the errors are logged to the server

  

Guess you like

Origin www.cnblogs.com/ms_senda/p/11518035.html
Recommended