gpt4 book ai didi

.net - 如何打破 Silverlight 中未处理的异常

转载 作者:行者123 更新时间:2023-12-04 01:48:10 25 4
gpt4 key购买 nike

在控制台 .Net 应用程序中,对于没有匹配 catch 块的异常,调试器在抛出点(堆栈展开之前)中断。 Silverlight 似乎在 try catch 中运行所有用户代码,因此调试器永远不会中断。相反,会引发 Application.UnhandledException,但在捕获异常并展开堆栈之后。为了在未处理的异常被抛出但未被捕获时中断,我必须启用第一次机会异常中断,这也会停止程序处理的异常。

有没有办法删除 Silverlight try 块,以便异常直接进入调试器?

最佳答案

实际上,这相当容易。

使用 Application_UnhandledException event您可以以编程方式inject a breakpoint .

using System.IO; // FileNotFoundException
using System.Windows; // Application, StartupEventArgs, ApplicationUnhandledExceptionEventArgs

namespace SilverlightApplication
{
public partial class App : Application
{
public App()
{
this.Startup += this.Application_Startup;
this.UnhandledException += this.Application_UnhandledException;

InitializeComponent();
}

private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new Page();
}

private void Application_UnhandledException(object sender,
ApplicationUnhandledExceptionEventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached)
{
// Break in the debugger
System.Diagnostics.Debugger.Break();

// Recover from the error
e.Handled = true;
return;
}

// Allow the Silverlight plug-in to detect and process the exception.
}
}
}

关于.net - 如何打破 Silverlight 中未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2814491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com