Created
September 7, 2012 21:52
-
-
Save phette23/3670033 to your computer and use it in GitHub Desktop.
JavaScript Assertion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// debugging technique | |
// via: http://www.learncomputer.com/javascript-tricks-you-may-not-know/ | |
function AssertException( msg ) { | |
this.msg = msg; | |
} | |
AssertException.prototype.toString = function() { | |
return 'AssertException: ' + this.msg; | |
} | |
function assert( exp, msg ) { | |
if ( !exp ) { | |
throw new AssertException( msg ); | |
} | |
} | |
// usage | |
// if first param evaluates to false then exception is thrown w/ 2nd param as message | |
try { | |
assert( obj != null, 'Object is null' ); | |
} catch( err ) { | |
if ( err instanceof AssertException ) { | |
// error handling | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment