C# 异常捕获失败?Visual Studio神秘地挂起
所以我已经定义了这个异常,受保护的构造函数,静态构造函数方法(所以所有的异常都是用相同的格式生成的,我不需要在构造函数中进行斗争。无论如何,这不是重点…(不是我的问题?让我们先别管它吧?) 在我的实现中,我有以下几点:C# 异常捕获失败?Visual Studio神秘地挂起,c#,.net,wpf,visual-studio,exception,C#,.net,Wpf,Visual Studio,Exception,所以我已经定义了这个异常,受保护的构造函数,静态构造函数方法(所以所有的异常都是用相同的格式生成的,我不需要在构造函数中进行斗争。无论如何,这不是重点…(不是我的问题?让我们先别管它吧?) 在我的实现中,我有以下几点: try { Save(); } catch (ValidationException) { UserMessage.CreateMessage("A Validation error has occured. Please contact your system ad
try
{
Save();
}
catch (ValidationException)
{
UserMessage.CreateMessage("A Validation error has occured. Please contact your system administrator / software support.").Show();
}
save函数基本上在NHibernate中完成我的所有最终操作,并在数据会话上执行Flush()
因此,基本上作为一个概述:
Save()
Flush()
在NHibernate中调用PreInsertEventListener
事件ValidationException.Create(…)
被调用,它返回一个ValidationException
。然后从PreInsertEventListener
catch
块触发,并捕获我的ValidationException
,提示我的用户消息对话框出现
但实际发生的情况是,vstudio和我的应用程序在调试中挂起约1分钟。100%无响应,忽略窗口信号,windows准备将进程踢到屁股上,然后它最终取消挂起并告诉我“引发了未处理的ValidationException”,正好在我抛出异常的那一行。所以不知怎么的,这并没有被发现
当我在调试器之外运行此程序时,应用程序insta将失败
我遗漏了什么吗?这一切都应该从同一个线程执行,调用堆栈非常清晰:
> MyApp.Core.dll!MyApp.Core.Validation.ValidationHelper.DoValidateObject(object obj = {MyApp.Data.Entities.Administration.Career}) Line 153 C#
MyApp.Data.Entities.dll!MyApp.Data.Entities.Entity.Validate(out System.Collections.Generic.IDictionary> validationErrors = null, bool throwExceptions = true) Line 73 + 0xa bytes C#
MyApp.Data.dll!MyApp.Data.NHibernate.EntityValidateListener.OnPreInsert(NHibernate.Event.PreInsertEvent event = {NHibernate.Event.PreInsertEvent}) Line 23 + 0x30 bytes C#
NHibernate.dll!NHibernate.Action.EntityInsertAction.PreInsert() Line 151 + 0x42 bytes C#
NHibernate.dll!NHibernate.Action.EntityInsertAction.Execute() Line 44 + 0xd bytes C#
NHibernate.dll!NHibernate.Engine.ActionQueue.Execute(NHibernate.Action.IExecutable executable = {EntityInsertAction[MyApp.Data.Entities.Administration.Career#24cda829-e829-4228-997f-55ff890d6eec]}) Line 130 + 0x37 bytes C#
NHibernate.dll!NHibernate.Engine.ActionQueue.ExecuteActions(System.Collections.IList list = Count = 1) Line 113 + 0x10 bytes C#
NHibernate.dll!NHibernate.Engine.ActionQueue.ExecuteActions() Line 146 + 0x13 bytes C#
NHibernate.dll!NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(NHibernate.Event.IEventSource session = {NHibernate.Impl.SessionImpl}) Line 240 + 0x3d bytes C#
NHibernate.dll!NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(NHibernate.Event.FlushEvent event = {NHibernate.Event.FlushEvent}) Line 19 + 0x1b bytes C#
NHibernate.dll!NHibernate.Impl.SessionImpl.Flush() Line 1187 + 0x92 bytes C#
MyApp.Data.dll!MyApp.Data.AbstractDataSession.Flush() Line 57 + 0x36 bytes C#
MyApp.UIFramework.dll!MyApp.UIFramework.ViewModel.EntitiesViewModel.Save() Line 110 + 0x2c bytes C#
ManagerWPF.exe!ManagerWPF.Modules.Careers.ViewModels.CareersViewModel.HandleSave() Line 274 + 0x11 bytes C#
ManagerWPF.exe!ManagerWPF.Modules.Careers.ViewModels.CareersViewModel.get_SaveCommand.AnonymousMethod(object x = null) Line 263 + 0xa bytes C#
MyApp.UIFramework.dll!MyApp.UIFramework.Commands.DelegateCommand.Execute(object parameter = null) Line 50 + 0x24 bytes C#
>MyApp.Core.dll!MyApp.Core.Validation.ValidationHelper.DoValidateObject(对象obj={MyApp.Data.Entities.Administration.Career})第153行#
MyApp.Data.Entities.dll!MyApp.Data.Entities.Entity.Validate(out System.Collections.Generic.IDictionary>validationErrors=null,bool throweexceptions=true)第73行+0xa字节C#
MyApp.Data.dll!MyApp.Data.NHibernate.EntityValidateListener.OnPreInsert(NHibernate.Event.PreInsertEvent Event={NHibernate.Event.PreInsertEvent})第23行+0x30字节C#
NHibernate.dll!NHibernate.Action.EntityInsertAction.PreInsert()行151+0x42字节C#
NHibernate.dll!NHibernate.Action.EntityInsertAction.Execute()行44+0xd字节C#
NHibernate.dll!NHibernate.Engine.ActionQueue.Execute(NHibernate.Action.IEExecutable executable={EntityInsertAction[MyApp.Data.Entities.Administration.Career.#24cda829-e829-4228-997f-55ff890d6eec]行130+0x37字节#
NHibernate.dll!NHibernate.Engine.ActionQueue.ExecuteActions(System.Collections.IList list=Count=1)第113行+0x10字节C#
NHibernate.dll!NHibernate.Engine.ActionQueue.ExecuteActions()行146+0x13字节C#
NHibernate.dll!NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(NHibernate.Event.IEventSource会话={NHibernate.Impl.SessionImpl})第240+0x3d字节C行#
NHibernate.dll!NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(NHibernate.Event.FlushEvent Event={NHibernate.Event.FlushEvent})第19行+0x1b字节C#
NHibernate.dll!NHibernate.Impl.SessionImpl.Flush()行1187+0x92字节C#
MyApp.Data.dll!MyApp.Data.AbstractDataSession.Flush()行57+0x36字节C#
MyApp.UIFramework.dll!MyApp.UIFramework.ViewModel.EntitiesViewModel.Save()行110+0x2c字节C#
ManagerWPF.exe!ManagerWPF.Modules.Careers.ViewModels.careerViewModel.HandleSave()行274+0x11字节C#
ManagerWPF.exe!ManagerWPF.Modules.Careers.ViewModels.careerViewModel.get_SaveCommand.AnonymousMethod(object x=null)第263+0xa行字节C#
MyApp.UIFramework.dll!MyApp.UIFramework.Commands.DelegateCommand.Execute(对象参数=null)行50+0x24字节C#
如果您确定所有事件都发生在同一个线程上(这是调用堆栈中的情况),那么也可能是名称空间冲突 正在抛出的
ValidationException
类型是否与应捕获的类型相同,即在同一命名空间中?似乎NHibernate已经定义了ValidationException
,您也定义了自己的ValidationException
要解决此问题,您有几个选项:
- 摆脱您自己的自定义异常,使用框架提供的异常
- 重命名您的异常类
- 使用完全限定名称,例如
MyApp.Exceptions.ValidationException
- 导入命名空间时使用别名:
使用MyEx=MyApp.Exceptions;
Save()的线程
@0xA3:正如我所说的,不是从另一个线程抛出的,我只是在.NET Reflector中遍历了NHibernate,以确保NHibernate没有出现任何异步调用。@0xA3,@Toby:我同意你们两个的观点,症状指向那个方向,但他似乎有一个清晰的调用堆栈,从他的函数开始对异常抛出代码的ion调用。@Aren:如果调用堆栈显示完整的调用路径,那么它就不会在另一个线程上执行。哦,天哪,我不敢相信它会这么简单。你完全是在想,在不同的命名空间中有一些附录代码,但名称相同(ValidationException
)。这段代码很久以前由另一位开发人员编写,计划删除。现在我想知道的是,当引发此异常时,为什么该死的vstudio会挂起1-2分钟?@ARE B:我不知道是什么让Visual Studio保持忙碌,但根据我的经验,Visual Studio调试器在遇到异常时反应缓慢是完全正常的一个未处理的异常。通常这是一个大约5-10秒和n的问题
try
{
Save();
}
catch (ValidationException)
{
UserMessage.CreateMessage("A Validation error has occured. Please contact your system administrator / software support.").Show();
}
> MyApp.Core.dll!MyApp.Core.Validation.ValidationHelper.DoValidateObject(object obj = {MyApp.Data.Entities.Administration.Career}) Line 153 C#
MyApp.Data.Entities.dll!MyApp.Data.Entities.Entity.Validate(out System.Collections.Generic.IDictionary> validationErrors = null, bool throwExceptions = true) Line 73 + 0xa bytes C#
MyApp.Data.dll!MyApp.Data.NHibernate.EntityValidateListener.OnPreInsert(NHibernate.Event.PreInsertEvent event = {NHibernate.Event.PreInsertEvent}) Line 23 + 0x30 bytes C#
NHibernate.dll!NHibernate.Action.EntityInsertAction.PreInsert() Line 151 + 0x42 bytes C#
NHibernate.dll!NHibernate.Action.EntityInsertAction.Execute() Line 44 + 0xd bytes C#
NHibernate.dll!NHibernate.Engine.ActionQueue.Execute(NHibernate.Action.IExecutable executable = {EntityInsertAction[MyApp.Data.Entities.Administration.Career#24cda829-e829-4228-997f-55ff890d6eec]}) Line 130 + 0x37 bytes C#
NHibernate.dll!NHibernate.Engine.ActionQueue.ExecuteActions(System.Collections.IList list = Count = 1) Line 113 + 0x10 bytes C#
NHibernate.dll!NHibernate.Engine.ActionQueue.ExecuteActions() Line 146 + 0x13 bytes C#
NHibernate.dll!NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(NHibernate.Event.IEventSource session = {NHibernate.Impl.SessionImpl}) Line 240 + 0x3d bytes C#
NHibernate.dll!NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(NHibernate.Event.FlushEvent event = {NHibernate.Event.FlushEvent}) Line 19 + 0x1b bytes C#
NHibernate.dll!NHibernate.Impl.SessionImpl.Flush() Line 1187 + 0x92 bytes C#
MyApp.Data.dll!MyApp.Data.AbstractDataSession.Flush() Line 57 + 0x36 bytes C#
MyApp.UIFramework.dll!MyApp.UIFramework.ViewModel.EntitiesViewModel.Save() Line 110 + 0x2c bytes C#
ManagerWPF.exe!ManagerWPF.Modules.Careers.ViewModels.CareersViewModel.HandleSave() Line 274 + 0x11 bytes C#
ManagerWPF.exe!ManagerWPF.Modules.Careers.ViewModels.CareersViewModel.get_SaveCommand.AnonymousMethod(object x = null) Line 263 + 0xa bytes C#
MyApp.UIFramework.dll!MyApp.UIFramework.Commands.DelegateCommand.Execute(object parameter = null) Line 50 + 0x24 bytes C#