views:

265

answers:

6

In console .Net applications, the debugger breaks at the point of the throw (before stack unwinding) for exceptions with no matching catch block. It seems that Silverlight runs all user code inside a try catch, so the debugger never breaks. Instead, Application.UnhandledException is raised, but after catching the exception and unwinding the stack. To break when unhandled exceptions are thrown and not catched, I have to enable first chance exception breaks, which also stops the program for handled exceptions.

Is there a way to remove the Silverlight try block, so that exceptions get directly to the debugger?

+3  A: 

In your web project, make sure the debugging of Silverlight applications checkbox is checked. You'll find the setting under the web application's Properties->Web tab.

In VS2008, hit Ctrl+Alt+E to bring up the Exceptions window, check the box under the Thrown column for "Common Language Runtime Exceptions". In VS2010, I don't believe the shortcut works, so you'll need to go to the Debug->Exceptions from the dropdown menu.

I'm not sure if this is exactly what you're looking for, but hopefully it helps!

greglev
Thanks for answering. I describe this workaround in my question. The problem with it is that the debugger stops the program even for exceptions that would be handled somewhere down the call stack.
Bruno Martinez
+1  A: 

The trouble maker is the DispatcherOperation.Invoke() method. It looks like this:

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

The "catch everything" clause prevents the debugger from stepping in. Silverlight is missing something similar to the Windows Forms' Application.SetUnhandledExceptionMode() method. And there's no check if a debugger is running, something else WF does.

This doesn't strike me as very hard to add, I'd recommend you post a feature request at connect.microsoft.com

Meanwhile, there is no other option available then Debug + Exceptions. Keep exceptions reserved for the truly exceptional to make that effective.

Hans Passant
A: 

I use the CTRL+ALT+E (Debug > Exceptions) method to force the debugger to break when thrown, but I do it on an as needed basis and as targeted as I can.

If I'm trying to track down an exception, I'll look for it's type in the Output Window [Debug] after the app crashes the first time. Then I'll turn on "break when thrown" for that exception type only by using the Find button on the right side of the dialog.

It's not perfect, but it's as filtered as I've gotten it.

Matt Dotson
+2  A: 

This is fairly easy, actually.

Making use of the Application_UnhandledException event you can programmatically 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.
        }
    }
}
hemp
Sorry - I just re-read the question and realized you already wrote off this approach as not meeting your needs. I'll leave the answer here, though, as someone else may find it helpful.
hemp
+1 didn't know about System.Diagnostics.Debugger.IsAttached
luvieere
A: 

Not every browser supports debugging Silverlight.

For example, I couldn't debug it with Firefox nor Chrome, it only worked correctly in IE. :(

If this is not your issue, just ignore this answer.

Venemo