【问题标题】:Neither Application.ThreadException nor AppDomain.CurrentDomain.UnhandledException are respectedApplication.ThreadException 和 AppDomain.CurrentDomain.UnhandledException 都不受尊重
【发布时间】:2011-07-22 21:21:04
【问题描述】:

首先,在Program.Program() 静态构造函数中,我有以下代码:

AppDomain.CurrentDomain.UnhandledException += (s, eargs) =>
    LogException((Exception)eargs.ExceptionObject);
Application.ThreadException +=
    (s, eargs) => LogException(eargs.Exception);

然后,如果我故意(例如)除以零,它就会被捕获。但是,线程循环中的其他异常都没有被捕获,就像这个 - 它的堆栈跟踪是:

System.ComponentModel.ReflectPropertyDescriptor.GetValue(component = {My.Component})    
System.Windows.Forms.BindToObject.GetValue()    
System.Windows.Forms.Binding.PushData(force)    
System.Windows.Forms.Binding.UpdateIsBinding()  
System.Windows.Forms.Binding.CheckBinding() 
System.Windows.Forms.Binding.SetListManager(bindingManagerBase) 
System.Windows.Forms.ListManagerBindingsCollection.AddCore(dataBinding = {System.Windows.Forms.Binding})    
System.Windows.Forms.BindingsCollection.Add(binding)    
System.Windows.Forms.BindingContext.UpdateBinding(newBindingContext, binding)   
System.Windows.Forms.Control.UpdateBindings()   
System.Windows.Forms.Control.OnBindingContextChanged(e = {System.EventArgs})    
System.Windows.Forms.Control.OnParentBindingContextChanged(e)   
System.Windows.Forms.Control.OnBindingContextChanged(e = {System.EventArgs})    
System.Windows.Forms.Control.OnParentBindingContextChanged(e)   
System.Windows.Forms.Control.OnBindingContextChanged(e = {System.EventArgs})    
System.Windows.Forms.Control.OnParentBindingContextChanged(e)   
System.Windows.Forms.Control.OnBindingContextChanged(e = {System.EventArgs})    
System.Windows.Forms.Control.OnParentBindingContextChanged(e)   
System.Windows.Forms.Control.OnBindingContextChanged(e = {System.EventArgs})    
System.Windows.Forms.Control.CreateControl()    
System.Windows.Forms.Control.SetVisibleCore(value = true)   
System.Windows.Forms.TabPage.Visible.set(value) 
System.Windows.Forms.TabControl.UpdateTabSelection(updateFocus = false) 
System.Windows.Forms.TabControl.OnHandleCreated(e)  
System.Windows.Forms.Control.WmCreate(m)    
System.Windows.Forms.Control.WndProc(m) 
System.Windows.Forms.TabControl.WndProc(m)  
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(m)   
System.Windows.Forms.Control.ControlNativeWindow.WndProc(m) 
System.Windows.Forms.NativeWindow.Callback(hWnd, msg = 0x00000001, wparam, lparam)  
[Native to Managed Transition]  
[Managed to Native Transition]  
System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(dwExStyle, lpszClassName, lpszWindowName, style, x, y, width, height, hWndParent, hMenu, hInst, pvParam)    
System.Windows.Forms.NativeWindow.CreateHandle(cp)  
System.Windows.Forms.Control.CreateHandle() 
System.Windows.Forms.TabControl.CreateHandle()  
System.Windows.Forms.Control.CreateControl(fIgnoreVisible = false)  
System.Windows.Forms.Control.CreateControl(fIgnoreVisible = false)  
System.Windows.Forms.Control.CreateControl(fIgnoreVisible = false)  
System.Windows.Forms.Control.CreateControl(fIgnoreVisible = false)  
System.Windows.Forms.Control.CreateControl()    
System.Windows.Forms.Control.WmShowWindow(m)    
System.Windows.Forms.Control.WndProc(m) 
System.Windows.Forms.ScrollableControl.WndProc(m)   
System.Windows.Forms.ContainerControl.WndProc(m)    
System.Windows.Forms.Form.WmShowWindow(m)   
System.Windows.Forms.Form.WndProc(m)    
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(m)   
System.Windows.Forms.Control.ControlNativeWindow.WndProc(m) 
System.Windows.Forms.NativeWindow.Callback(hWnd, msg = 0x00000018, wparam, lparam)  
[Native to Managed Transition]  
[Managed to Native Transition]  
System.Windows.Forms.Control.SetVisibleCore(value = true)   
System.Windows.Forms.Form.SetVisibleCore(value = true)  
System.Windows.Forms.Control.Visible.set(value) 
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(reason = 0xffffffff, context = {System.Windows.Forms.ApplicationContext})    
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(reason, context)  
System.Windows.Forms.Application.Run(mainForm)  
My.Namespace.Program.Main()
[Native to Managed Transition]  
[Managed to Native Transition]  
System.AppDomain.ExecuteAssembly(assemblyFile, assemblySecurity, args)  
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()   
System.Threading.ThreadHelper.ThreadStart_Context(state)    
System.Threading.ExecutionContext.Run(executionContext, callback, state)    
System.Threading.ThreadHelper.ThreadStart() 

ReflectPropertyDescriptor 调用我的代码并且我抛出一个自定义异常时,系统会默默吞下它,我无法记录它。

【问题讨论】:

  • 您是在 64 位操作系统上运行它吗? Ctrl+F5启动时会不会爆炸?
  • 是的……为什么?这会以某种方式干扰异常处理吗?
  • 不,在不调试的情况下运行不会失败。内部会抛出异常,但没有一个是未处理的。
  • 这是一个众所周知的问题,调试器和 Windows 之间的不愉快交互,他们无法弄清楚如何修复。项目 + 属性,编译选项卡,平台目标 = x86 来解决它。保持 Release 版本的设置不变是可以的。
  • 这不是问题。它也发生在 release+x86 上。

标签: c# exception-handling


【解决方案1】:

事实证明,我丢失的大部分异常都可以在BindingComplete 事件中看到。必须这样做。

【讨论】:

猜你喜欢
  • 2014-06-02
  • 2011-01-02
  • 1970-01-01
  • 2012-01-10
  • 2020-02-21
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
  • 2010-10-05
相关资源
最近更新 更多