简体   繁体   English

C#使用反射来捕获异常引发

[英]C# use reflection to capture exception throw

Is there a way using reflection or another methodology inside a catch block to get the exception type that has been thrown, along with the stack trace without appending the catch(Exception ex) parameter? 有没有一种方法可以在catch块中使用反射或其他方法来获取已引发的异常类型以及堆栈跟踪,而无需附加catch(Exception ex)参数?

I want a way catch all exceptions and mitigate the code analysis finding for overly broad catch. 我想要一种捕获所有异常并减轻代码分析发现范围的方法。

catch
{
     //Include Reflection here to get error thrown information

     divRecordNotFound.Visible = true;
     labelRecordNotFound.Text = string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}{1}", ValidNS.Properties.Resoures.ErrorValue); 
     divRecordFound.Visible = false;
}

By doing a catch (Exception ex) you also catch all exceptions which derive from Exception . 通过执行catch (Exception ex)您还可以捕获所有从Exception派生的Exception If you then want the type of it, you can just call ex.GetType() . 如果随后需要它的类型,则只需调用ex.GetType() Hope it helped ;) 希望它有所帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM