即使在 C# 代码中处理,InvalidOperationException 也会导致中断 Visual Studio

InvalidOperationException causing break in Visual Studio even when handled in C# code

在我的 C# 应用程序中,我正在捕获 SystemException 类型的异常,但是 Visual Studio 2015 在引发异常时中断:

如您在上面的屏幕截图中所见,我有意在某些对象初始化之前提前释放 ISupportInitialize 锁。我期待异常,并且有一个 catch 块。

另外,我感兴趣的异常设置中没有列出window;也就是说,他们没有被检查进入中断模式。

虽然对话框显示 "Break when this exception is thrown" 未选中,但应用程序仍会进入中断模式。

编辑:

SLaks 请求的堆栈跟踪:

PresentationCore.dll!System.Windows.Media.GlyphRun.CheckInitialized() 未知 PresentationCore.dll!System.Windows.Media.GlyphRun.ComputeInkBoundingBox() 未知 PresentationCore.dll!System.Windows.Media.BoundsDrawingContextWalker.DrawGlyphRun(System.Windows.Media.Brush foregroundBrush, System.Windows.Media.GlyphRun glyph运行) 未知 PresentationCore.dll!System.Windows.Media.RenderData.DrawingContextWalk(System.Windows.Media.DrawingContextWalker ctx) 未知 PresentationCore.dll!System.Windows.Media.RenderData.GetContentBounds(System.Windows.Media.BoundsDrawingContextWalker ctx) 未知 PresentationCore.dll!System.Windows.UIElement.GetHitTestBounds() 未知 PresentationCore.dll!System.Windows.Media.Visual.PrecomputeContent() 未知 PresentationCore.dll!System.Windows.Media.Visual.PrecomputeRecursive(out System.Windows.Rect bboxSubgraph)未知 PresentationCore.dll!System.Windows.Media.Visual.PrecomputeRecursive(out System.Windows.Rect bboxSubgraph)未知 PresentationCore.dll!System.Windows.Media.Visual.PrecomputeRecursive(out System.Windows.Rect bboxSubgraph)未知 PresentationCore.dll!System.Windows.Media.Visual.PrecomputeRecursive(out System.Windows.Rect bboxSubgraph)未知 PresentationCore.dll!System.Windows.Media.Visual.PrecomputeRecursive(out System.Windows.Rect bboxSubgraph)未知 PresentationCore.dll!System.Windows.Media.Visual.PrecomputeRecursive(out System.Windows.Rect bboxSubgraph)未知 PresentationCore.dll!System.Windows.Media.Visual.PrecomputeRecursive(out System.Windows.Rect bboxSubgraph)未知 PresentationCore.dll!System.Windows.Media.Visual.Precompute() 未知 PresentationCore.dll!System.Windows.Media.CompositionTarget.System.Windows.Media.ICompositionTarget.Render(bool inResize, System.Windows.Media.Composition.DUCE.Channel channel) 未知 PresentationCore.dll!System.Windows.Media.MediaContext.Render(System.Windows.Media.ICompositionTarget resizedCompositionTarget) 未知 PresentationCore.dll!System.Windows.Media.MediaContext.RenderMessageHandlerCore(object resizedCompositionTarget) 未知 PresentationCore.dll!System.Windows.Media.MediaContext.RenderMessageHandler(object resizedCompositionTarget) 未知 WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate 回调,对象参数,int numArgs) 未知 WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源,System.Delegate 回调,对象参数,int numArgs,System.Delegate catchHandler)未知 WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeImpl() 未知 WindowsBase.dll!System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象状态)未知 mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态, bool preserveSyncCtx) 未知 mscorlib.dll!System.Threading.ExecutionContext.运行(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态, bool preserveSyncCtx) 未知 mscorlib.dll!System.Threading.ExecutionContext.运行(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态)未知 WindowsBase.dll!MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态)未知 WindowsBase.dll!System.Windows.Threading.DispatcherOperation.Invoke() 未知 WindowsBase.dll!System.Windows.Threading.Dispatcher.ProcessQueue() 未知 WindowsBase.dll!System.Windows.Threading.Dispatcher.WndProcHook(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) 未知 WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) 未知 WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象 o) 未知 WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate 回调,对象参数,int numArgs) 未知 WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源,System.Delegate 回调,对象参数,int numArgs,System.Delegate catchHandler)未知 WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority 优先级,System.TimeSpan 超时,System.Delegate 方法,对象参数,int numArgs) 未知 WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) 未知 [从原生到托管的过渡]
[管理到本机过渡]
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame帧)未知 WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame帧)未知 PresentationFramework.dll!System.Windows.Application.RunDispatcher(对象忽略)未知 PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window window) 未知 PresentationFramework.dll!System.Windows.Application.Run(System.Windows.Window window) 未知 PresentationFramework.dll!System.Windows.Application.Run() 未知 FargoApp.exe!FargoApp.App.Main() 未知 [从原生到托管的过渡]
[管理到本机过渡]
mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args) 未知 Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 未知 mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(对象状态)未知 mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态, bool preserveSyncCtx) 未知 mscorlib.dll!System.Threading.ExecutionContext.运行(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态, bool preserveSyncCtx) 未知 mscorlib.dll!System.Threading.ExecutionContext.运行(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态)未知 mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() 未知

您无法捕获异常可能是因为它发生在另一个线程中。对于这种情况,您可以将事件处理程序添加到 App.xaml.cs 中的 DispatcherUnhandledException 事件,如下所示:

DispatcherUnhandledException += (sender, args) =>
            {
                Console.WriteLine("Wild exception appeared!!!");
            };

这样您就可以确保您的应用程序抛出但未处理的异常将触发此处理程序。更多信息 here

希望对您有所帮助!