我正在寻找一种方法来检索当前异常,而不必将其作为变量传递。
假设以下代码
public void MakeItFail()
{
try
{
throw new FailException();
}
catch // Yes I'm aware that this shouldn't be done, but I don't want to go through all the code base and change it
{
ShowMessage("An error occured");
}
}
public void ShowMessage(string message)
{
// How can I retrieve the exception here
}
在监视窗口中,我可以使用$ exception来获取当前异常。是否有相同的代码?
答案 0 :(得分:8)
不,没有。
您需要使用参数。
答案 1 :(得分:3)
首次加载应用时,请尝试订阅此活动。
AppDomain.CurrentDomain.FirstChanceException += (s, e) =>
{
ShowMessage(e.Exception.Message);
};