本文主要是介绍uni-app学习笔记之js异常处理try-catch-finally流程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 无finally或者finally不带return的情况下,try顺利执行则返回try—ok
testFunc() {var res = this.demo()console.log(res);},demo() {try {var x = "ok"if (x == "err") throw "ERROR";return '1---ok';} catch (e) {console.log(e);return '2---err';} finally {console.log('finally');// return '3---finally';}return '4---end';}
21:14:33.940 finally at pages/test/test.vue:28
21:14:33.958 1---ok at pages/test/test.vue:17
2. 无finally或者finally不带return的情况下,try抛出异常则返回catch—err
testFunc() {var res = this.demo()console.log(res);},demo() {try {var x = "err"if (x == "err") throw "ERROR";return '1---ok';} catch (e) {console.log(e);return '2---err';}finally {console.log('finally');// return '3---finally';}return '4---end';}
21:22:09.763 ERROR at pages/test/test.vue:25
21:22:09.769 finally at pages/test/test.vue:29
21:22:09.773 2---err at pages/test/test.vue:17
3. 只要finally带上return,优先级高于其他任何return,必然返回3—finally
testFunc() {var res = this.demo()console.log(res);},demo() {try {var x = "err"if (x == "err") throw "ERROR";return '1---ok';} catch (e) {console.log(e);return '2---err';}finally {console.log('finally');return '3---finally';}return '4---end';}
21:25:13.191 ERROR at pages/test/test.vue:25
21:25:13.197 finally at pages/test/test.vue:29
21:25:13.202 3---finally at pages/test/test.vue:17
总结:
不带finally的try—catch异常处理双return模式,返回值可以对流程处理进行判断
这篇关于uni-app学习笔记之js异常处理try-catch-finally流程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!