Thread was being aborted 分析与解决

xiaoxiao2021-02-27  209

Thread was being aborted 分析与解决

在捕获异常的try块中使用 response.redirect(); 或 response.write();response.end(); 有时会提示 线程已被中止(英文:"Thread was being aborted")错误. 分析该错误的原因是由于执行这两个命令会重新发起一次请求,将当前请求的进程abort掉;通俗点讲就是当进程还想继续执行的时候, 发现自己已经被调用过Abort方法了. 既然自己作为线程已经被中止, 就无法执行了, 于是exception丢了出来。 解决方法有如下三种: 1、将这两个命令放到try/catch块外,不捕获异常就不会提示这个错误; 2、捕获异常时进行判断: try {} catch(Exception ex) { if(!(ex is System.Threading.ThreadAbortException)) { //在这里显示错误 } } 3、捕获进程终止错误不做处理 try {} catch (System.Threading.ThreadAbortException) {           //忽略 } catch(Exception ex) { //显示错误 }
转载请注明原文地址: https://www.6miu.com/read-11537.html

最新回复(0)