rapale's blog
Do one thing with your heart

前端备忘录

前端细节 总结沉淀

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JS中标准报错处理通过 try catch finally ,使用格式

try {


} catch (err) {


} finally {


}

 

代码1:

try {
  console.log('顺序 1')
} catch (err) {
  console.log('顺序 2 :接收到报错原因:', err.message)
} finally {
  console.log('顺序 3 :不管有没有异常都会执行')
}

执行结果:

顺序 1

顺序 3 :不管有没有异常都会执行

 

代码2:

try {
  console.log('顺序 1', a)
} catch (err) {
  console.log('顺序 2 :接收到报错原因:', err.message)
} finally {
  console.log('顺序 3 :不管有没有异常都会执行')
}

执行结果:

顺序 2 :接收到报错原因: a is not defined

顺序 3 :不管有没有异常都会执行

 

代码3:

try {
  console.log('顺序 1')
  throw "报错原因";
} catch (err) {
  console.log('顺序 2 :接收到报错原因:', err)
} finally {
  console.log('顺序 3 :不管有没有异常都会执行')
}

执行结果:

顺序 1

顺序 2 :接收到报错原因: 报错原因

顺序 3 :不管有没有异常都会执行

posted on 2021-01-13 14:51  rapale  阅读(1397)  评论(0编辑  收藏  举报