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

Bru*_*nez 11 .net silverlight exception-handling

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

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

hem*_*emp 9

实际上,这很容易.

利用Application_UnhandledException事件,您可以以编程方式注入断点.
 

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.
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Gre*_*gen 5

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

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

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