throw exception这么基本的微信小程序都不行!!!
对于特定的情况需要抛出异常中断方法的执行并对其进行处理,发现定义的异常抛出后,无法利用try…catch…捕获!!!,什么鬼!!!
对于特定的情况需要抛出异常中断方法的执行并对其进行处理,发现定义的异常抛出后,无法利用try…catch…捕获!!!,什么鬼!!!
会有这个还是因为我们团队发现在弱网情况下(IOS开发者工具very bad network),一次请求多个接口时(只是这种情况还是普遍情况就不知道了),json中设置的
"networkTimeout": { "request": 10000, "downloadFile": 10000
},
无效,实际请求的超时并没有收到异常提示,因此自己想计时抛出异常。
function tryTestThrowException() { try{ testThrowException(); } catch (err) { console.log(e); }}function testThrowException() { let sec = 0; const timer = setInterval(() => { sec++; if (sec > 5) { console.info('即将清除timer'); clearInterval(timer); console.info('已经清除timer'); throw new Error('网络超时'); } }, 1000);} |
Page({ onShow: function () { try { testThrowException(); } catch (err) { console.log('err: ', err); } tryTestThrowException(); }}) |
兄弟,你这是异步事件里面的异常,需要在异步里捕获。
testThrowException: function() { let sec = 0; const timer = setInterval(() => { sec++; if (sec > 5) { console.info('即将清除timer'); clearInterval(timer); console.info('已经清除timer'); try{ throw new Error('网络超时'); } catch (err){ console.log(err) } } }, 1000); }, |