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 开发者_如何学Pythonnever 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?
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.
}
}
}
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!
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 Winforms 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 than Debug + Exceptions, tick the Thrown checkbox to force the debugger to stop when the exception is thrown. Keep exceptions reserved for the truly exceptional.
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.
Enabling "Just My Code" debugging is close to the behavior I want. See
http://forums.silverlight.net/forums/p/20678/72192.aspx
and
https://connect.microsoft.com/VisualStudio/feedback/details/356687/visual-studio-breaks-on-unhandled-exceptions-incorrectly-in-silverlight#tabs
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.
click debug, choose exceptions, mark common language runtime exceptions as thrown. I had the same problem and it fixed the problem for me
精彩评论