2010-12-28 19 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"); 

Wie wird das verwendet?Kann mir jemand diesen JavaScript-Code erklären?

+2

Sind Sie mit dem Auslösen von Ausnahmen vertraut? Ich versuche herauszufinden, auf welcher Ebene ich dies erklären kann. –

+0

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

+1

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

Antwort

2

So können Sie Objekte in Javascript erstellen, in diesem Fall ein UserException Objekt mit einer Funktion. Es könnte wie folgt verwendet werden:

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

Ich bekomme es! VIELEN DANK!!!! – DarkLightA

Verwandte Themen