angular8 自定义异常类型 作者:七棵菜 日期:2022-11-30 栏目:计算机分类:1 人气:428 ### 自定义异常 ``` /** * 接口异常 * 继承自 Error */ export class InvalidApiError extends Error { constructor(message: string) { super(message); Object.setPrototypeOf(this, new.target.prototype); } } /** * 授权异常 * 继承自 InvalidApiError */ export class UnauthorizedApiError extends InvalidApiError { constructor(message: string) { super(message); Object.setPrototypeOf(this, new.target.prototype); } } ``` ### 抛出异常 ``` const message = '授权已过期'; const api_error = new UnauthorizedApiError(message); throw api_error; ``` ### 捕获异常 ``` handleError(error: any): void { if (error instanceof UnauthorizedApiError) { // 授权异常,退出登录 console.log('授权异常,请退出重新登录'); } } ``` 标签: 自定义异常 捕获自定义异常 上一篇:如何使用mvn命令导入依赖 下一篇:angular8 使用toPromise后,异常总是:Uncaught (in promise) 随便看看 2024-02-19 PHP7 运算符“??” 和“?:”的区别 2022-11-30 Linux 后台运行命令 2022-11-25 关于我们 2022-11-30 centos一键系统安装lnmp集成环境 2022-11-30 linux 生成 ssh 公钥 留言