2013-03-30 3 views
0

나는 몇 가지 유용한 디버그 정보와 사용자 정의 예외를 만들려고 해요 :검사 사용자 정의 예외

var customError = function(name, message) { 

    this.message = message; 
    this.name = name; 

    this.prototype = new Error(); // to make customError "inherit" from the 
            // default error 
}; 

throw new customError('CustomException', 'Something went wrong'); 

을하지만 내가 갖는 모든 콘솔에 퍼지 메시지입니다 :

IE " SCRIPT5022 : 예외가 발생하고 catch되지 "

파이어 폭스 :"캐치되지 않는 예외 : [개체 개체] "

예외 상황에서 유용한 정보를 얻으려면 무엇을 변경해야합니까? 프로토 타입의

답변

1

잘못된 사용 :

var customError = function(name, message) {  
    this.message = message; 
    this.name = name; 
}; 

customError.prototype = new Error(); // to make customError "inherit" from the 
            // default error 

throw new customError('CustomException', 'Something went wrong'); 

prototype 생성자 (customError)의 속성입니다 아닌 구성 객체 (this)의, ECMA 초 4.2.14.3.5를 참조하십시오.

+0

감사합니다. IE는 여전히 나에게 동일한 비범 한 메시지를 제공합니다. 아마도 그것은 IE의 모든 단점 중 하나입니다 :) – Johan

+0

Btw,'customError.prototype.constructor = customError'를 추가해야합니까, 아니면 필요하지 않습니까? – Johan

+0

@Johan : 일반적으로 필요하지 않습니다. 나는 그런 것을 한 번 보았다고 믿는다. 그러나 나는 어디에서 기억할 수 없다. 한편, http://stackoverflow.com/questions/402538/convention-for-prototype-inheritance-in-javascript를 살펴보십시오. 그것은 일반적으로 비표준입니다. – Zeta