Skip to content

Instantly share code, notes, and snippets.

@phette23
Created September 7, 2012 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phette23/3670033 to your computer and use it in GitHub Desktop.
Save phette23/3670033 to your computer and use it in GitHub Desktop.
JavaScript Assertion
// 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