Conditional Error Handling in Javascript -
when deciding if ignore, handle of re-throw errors in try catch structure, best way uniquely identify exact error?
i can't find standard error number, have parse name , message properties?
edit: in illustration below, want check if error due lack of property on listevent who's key value of evnt.type. if source of error, want ignore it, otherwise want re-throw allow bubble up.
the way can think of sort of duck-typing error this...
try{ listevent[evnt.type](procedure) }catch(error){ if (error.message.match(evnt.type)) { console.log(error.name + ' ' + error.message + ' - ignored') } else { throw error } }
create custom exception , utilize instanceof:
function illegalargumentexception(message) { this.message = message; } you want create extend error prototype:
illegalargumentexception.prototype = new error(); illegalargumentexception.prototype.constructor = error; which can utilize so:
throw new illegalargumentexception("argument cannot less zero"); you can check type using instanceof:
try { // code generates exceptions } grab (e) { if (e instanceof illegalargumentexception) { // handle } else if (e instanceof someothertypeofexception) { // handle } } you can add together other property want constructor of exception.
as far illustration goes, i'm not sure you're trying do. listevent[evnt.type] homecoming undefined if event.type not property or key in listevent. improve see if evnt.type exists doing this:
if (typeof listevent[evnt.type] !== "undefined") { listevent[evnt.type](procedure); } javascript error-handling
No comments:
Post a Comment