相关疑难解决方法(0)

C#:从变量重新抛出异常,同时保留堆栈跟踪

在我的代码库中,我有一些重试功能,它将异常存储在变量中,最后在变量中抛出异常。想象一下这样的事情

Exception exc = null;

while(condition){
   try {
      // stuff
   } catch (Exception e){
     exc = e;
   }
}

if (e != null){
   throw e;
}
Run Code Online (Sandbox Code Playgroud)

现在,由于这是一条throw e语句而不是一条throw语句,因此原始堆栈跟踪丢失了。有没有办法进行重新抛出以保留原始堆栈跟踪,或者我需要重组我的代码以便我可以使用语句throw?

.net c# exception

3
推荐指数
1
解决办法
1155
查看次数

标签 统计

.net ×1

c# ×1

exception ×1