This article from Clientside discusses three types of error handling in JavaScript:
- Graceful: if possible, just ignore the error and continue with some default state or without a meaningful value
- Debug: throw a warning to the dbug.log method but continue otherwise
- Break: Either explicitly throw an error or (more often) just let the error that is thrown at runtime be thrown
Graceful Examples
Graceful degradation includes some default behavior that occurs when data is missing or an expected result does not occur.
this.id = this.options.id || 'StickyWin_'+new Date().getTime();
A common practice of graceful error handling is to include Try/Catch statements
getContent: function(){
try {
new Ajax((this.options.ajaxLink || this.options.observer.href), $merge(this.options.ajaxOptions, {
onComplete: this.show.bind(this)
})
).request();
} catch(e) {
dbug.log('ajax error on PopupDetail: %s', e);
}
}Debug Example - warning the user
var ln = this.options.ajaxLinks.length;
if(ln <= 0) ln = this.options.details.length;
if (this.options.observers.length != ln)
dbug.log("warning: observers and details are out of sync.");
Break Example - toss that exception
ret = Date.$months[month - 1] || false;
if (!ret) throw new Error('Invalid month index value must be between 1 and 12:' + index);
Delicious
Digg
StumbleUpon
Propeller
Reddit
Magnoliacom
Newsvine
Furl
Facebook
Google
Yahoo
Technorati
Icerocket
Post new comment