Error Handling in JavaScript

by Po-Jung Chen

JavaScript

function DivisionByZeroError(message) {
  this.name = 'DivisionByZeroError';
  this.message = message;
}

DivisionByZeroError.prototype = new Error();
DivisionByZeroError.prototype.constructor = DivisionByZeroError

DivisionByZeroError.prototype.showError = function() {
  return this.name + ': "' + this.message + '"';
}

let denominator = 0

function tryCatch () {
  try {
    if (denominator === 0) {
      throw new DivisionByZeroError("Attempted division by zero!");
    }
  }
  catch (e) {
  	// debugger
  	console.log(e.constructor)
    // console.log(e.showError())
  }
}

tryCatch()