importPolicemanList({ fileId: fileId[0].id }).then(()=>{ this.$message.success("导入成功"); }); (正确,能显示导入成功) --------------------上下两者等价吧,为何下面的当时不显示导入成功------------------ try{ await importPolicemanList({ fileId: fileId[0].id }) this.$message.success("导入成功"); }catch(err){ this.$message.error(`${err}`); }(错误,什么都不显示) 希望能得到正确的结果!
如何优雅地细粒度管理错误捕获? 比如我有下面这段代码 async function xxx() { const result1 = await getInfo1() const result2 = await getInfo2(paramsBasedResult1) ... } 错误捕获? async function xxx() { try { const result1 = await getInfo1() try { const result2 = await getInfo2(paramsBasedResult1); .... }catch(error) { ... } }catch(error) { .... } } 一层套一层给我感觉就像是回调地狱。 以我这段代码为例(下面是我做的错误处理的方式,可以将try catch去掉,去实现你自己的错误捕获处理) async function loadChart() { // 1. 获取管理区代码 // 2. 拼接管理区代码,发送请求 // 3. 处理数据 // option.xAxis.data = [] option.series = [] try { const params1 = { gs_id: 'xxNnrViL4f', p_id: 'xxNnrViL4f' }; const result1 = await getOrgTree(params1); if (!result1.isSucceed) throw new Error(result1.errMsg); try{ const mergeGlqdm = result1.data[0].datas.reduce((pre, cur) => pre + "," + cur.code, "").slice(1); const params2 = { glqdm: mergeGlqdm, xmdm: props.xmdm, ksrq: props.ksrq, jsrq: props.jsrq } const result2 = await getChartData(params2); if (!result2.isSucceed) throw new Error(result2.errMsg); const data = result2.data[0].datas; let preRq = "9999-99-99"; let isCollectAllDate = false; for (const item of data) { if (item.rq > preRq) { option.series[option.series.length - 1].data.push(item.zbz); } else { option.series.push({ data: [item.zbz], type: "line", name: `系列${option.series.length + 1}` }) if (preRq !== "9999-99-99") { isCollectAllDate = true; } } if (!isCollectAllDate) option.xAxis.data.push(item.rq); preRq = item.rq; } myChart.setOption(option); }catch(error) { ElMessage.error("获取曲线数据失败,错误信息:" + error.message) } }catch(error) { ElMessage.error("获取分管理区数据失败,错误信息:" + error.message); } }
定义一个简单的异常类: "class myException(Exception): pass" 定义一个函数 def f1(): try: print(1/0) except: raise myException('my exception') 运行f1() Traceback (most recent call last): File "", line 3, in f1 ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "", line 5, in f1 __main__.myException: my exception 写一段代码调用f1(): try: f1() except myException as e: print(e) 为何输出结果仅仅有 my exception 下面这些信息为何不出现? Traceback (most recent call last): File "", line 3, in f1 ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "", line 5, in f1 __main__.myException: my exception