2010-12-28 22 views
0
// Create an object type UserException 
function UserException (message){ 
    this.message=message; 
    this.name="UserException"; 
} 

// Make the exception convert to a pretty string when used as 
// a string (e.g. by the error console) 
UserException.prototype.toString = function(){ 
    return this.name + ': "' + this.message + '"'; 
} 

// Create an instance of the object type and throw it 
throw new UserException("Value too high"); 

これはどのように使用されますか?誰かがこのJavaScriptコードを私に説明できますか?

+2

例外をスローするのは慣れていますか?私はこれを説明するレベルを決定しようとしています。 –

+0

http://stackoverflow.com/questions/464359/custom-exceptions-in-javascript – jball

+1

http://en.wikipedia.org/wiki/Exception_handling –

答えて

2

これは、javacriptでオブジェクトを作成する方法です。この場合、UserExceptionオブジェクトにはtoStringという機能があります。

try { 
    throw new UserException("something went wrong"); 
} catch(ex) { 
    console.log(ex); 
} 
+0

私はそれを手に入れよう!感謝!!!! – DarkLightA

関連する問題