Js自定义异常

function MyError(msg){
  this.name="MyError";
  this.message = msg || "自定义异常的默认消息";
}

MyError.prototype = Object.create(Error.prototype);

MyError.prototype.constructor = MyError

try{

  throw new MyError("xiaol")

}catch(e){

  console.log(e.name+":"+e.message)

}

原文地址:https://www.cnblogs.com/413xiaol/p/6765228.html