JS Error Management

The error constructor creates an error object. Instances are thrown when runtime errors occur. Usually you create an Error object with the intenction of raising it using the throw keyword. You can handle the error using the try...catch construct

try { throw new Error('Gosh Darnit!'); } catch (e) { console.log(e.name + ': ' + e.message); }


You can also create your own custom Error classes.

class CustomError extends Error { constructor(foo = 'bar', ...params) { //Pass remaining arguments (including vendor specific ones) to parent constructor super(...params); //Custom debugging information this.foo = foo; this.date = new Date(); } } try { throw new CustomError('baz', 'bazMessage'); } catch(e){ console.log(e.foo); //baz console.log(e.message); //bazMessage console.log(e.stack); //stacktrace }