如何在不传递变量的情况下获取当前exception?

我正在寻找一种方法来检索当前的exception,而不必将其作为变量传递。

假设以下代码

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来获取当前exception。 是否有相同的代码?

不,没有。

您需要使用参数。

首次加载应用时,请尝试订阅此活动。

 AppDomain.CurrentDomain.FirstChanceException += (s, e) => { ShowMessage(e.Exception.Message); };