如何在Silverlight中打破未处理的异常

时间:2010-05-11 21:12:42

标签: .net silverlight exception-handling

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

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

7 个答案:

答案 0 :(得分:9)

实际上这很容易。

您可以通过编程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.
        }
    }
}

答案 1 :(得分:5)

在您的Web项目中,确保选中Silverlight应用程序的调试复选框。您可以在Web应用程序的Properties-> Web选项卡下找到该设置。

在VS2008中,按Ctrl + Alt + E打开“例外”窗口,选中“公共语言运行时例外”的“投掷”列下的框。在VS2010中,我不相信快捷方式有效,因此您需要从下拉菜单中转到Debug-> Exceptions。

我不确定这是否正是您正在寻找的,但希望它有所帮助!

答案 2 :(得分:4)

麻烦制造者是DispatcherOperation.Invoke()方法。它看起来像这样:

internal void Invoke()
{
    try
    {
        this._operation.DynamicInvoke(this._args);
    }
    catch (Exception exception)
    {
        Error.GetXresultForUserException(exception);
    }
}

“catch everything”子句阻止调试器进入.Silverlight缺少类似于Windows窗体的Application.SetUnhandledExceptionMode()方法。并且没有检查调试器是否正在运行,Winforms还有其他功能。

这并不会让我觉得很难添加,我建议您在connect.microsoft.com上发布功能请求

同时,除了Debug + Exceptions之外没有其他选项可用,勾选Thrown复选框以强制调试器在抛出异常时停止。保留为真正例外保留的例外情况。

答案 3 :(得分:1)

我使用CTRL + ALT + E(Debug> Exceptions)方法强制调试器在抛出时中断,但我会根据需要和尽可能的目标执行此操作。

如果我正在尝试追踪异常,我会在应用程序第一次崩溃后在输出窗口[Debug]中查找它的类型。然后,我将仅使用对话框右侧的“查找”按钮为该异常类型打开“抛出时中断”。

它并不完美,但它已经过滤了我已经过滤了。

答案 4 :(得分:0)

答案 5 :(得分:0)

并非每个浏览器都支持调试Silverlight。

例如,我无法使用Firefox或Chrome调试它,它只能在IE中正常工作。 :(

如果这不是您的问题,请忽略此答案。

答案 6 :(得分:0)

单击debug,选择exception,将公共语言运行时异常标记为thrown。 我有同样的问题,它解决了我的问题