Search code examples
silverlightdebuggingvisual-studio-debugging

Silverlight errors showing in JavaScript, not Visual Studio


Building a Silverlight 5 app that runs in the browser but getting some nagging errors that are showing up in JavaScript, they aren't being caught by Visual Studio for some reason when the debugger is attached. I've done the simple stuff (create VS SL 5 project w/ a web project). When I'm debugging, the errors are showing up in the browser via JS... not in Visual Studio.

I can set breakpoints and walk through them, but when an unhandled exception happens, SL is just bubbling it up through the page (as expected), but VS isn't catching it.

I've verified that VS is attached to the IE process and the Type=Silverlight. Right now the bug I'm fighting with is the nagging 4004 error code (category=managedruntimeerror) with the message of "system.argumentexception: value does not fall within the expected range" but I can't find where that is happening w/o a stack more detail. All I get is the "Visual Studio JIT debugger" that looks very much like this http://www.scottleckie.com/2010/04/code-4004-unhandled-error-in-silverlight-application/ but his solution isn't working for me...


Solution

  • Look in your App.xaml.cs and you will find a handler called Application_UnhandledException.
    There should be some code similar to this:

        Deployment.Current.Dispatcher.BeginInvoke(delegate
        {
            ReportErrorToDOM(e);
        });
    

    Replace it with your custom error handling method.
    The ReporErrorToDOM is what triggers the javascript error in your browser.


    If there isn't one you need to set one up:

    public App()
    {
        this.UnhandledException += this.Application_UnhandledException;
        ...
    }
    private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    {
        // Handle exception here
        ...
        // Don't forget this !
        e.Handled = true;
    }