[英]ApplicationException not being thrown to catch block properly
I am having an issue where I override the OnValidate
method of a LINQ to SQL class. 我在重写LINQ to SQL类的
OnValidate
方法时遇到问题。 I have written my business rules properly and all is well, but when I go to catch the exception in the call to my repository, the application is treating the Exception as unhandled. 我已经正确地编写了业务规则,并且一切都很好,但是当我在对存储库的调用中捕获异常时,应用程序将异常视为未处理。
In the LINQ to SQL partial class I have the following: 在LINQ to SQL子类中,我具有以下内容:
partial void OnValidate(ChangeAction action)
{
if (!IsValid)
//Application is halting here before my catch block code is reached
throw new ApplicationException("Invalid note data recieved! Correct the following issues and try again:");
//UpdateTimeStamp();
}
The IsValid
is just a set of checks on the LINQ to SQL class properties. IsValid
只是对LINQ to SQL类属性的一组检查。
In my Form I have a button click event that calls the SubmitChanges
method on my repository class. 在我的表单中,我有一个按钮单击事件,该事件在我的存储库类上调用
SubmitChanges
方法。 I have wrapped it in a try/catch
block but the Exception will not bubble up to my catch
instead it errors at the throw new ApplicationException()
block in my partial class. 我已经将其包装在
try/catch
块中,但是Exception不会冒到我的catch
而是在我的局部类中throw new ApplicationException()
块时出错。
Here is the code around the Button: ( Repository
is an injected implementation of an IRepository
). 以下是Button周围的代码:(
Repository
是IRepository
的注入实现)。
private void SubmitButton_Click(object sender, EventArgs e)
{
try
{
if (IsNewNote)
{
//CurrentNote is an instance of my LINQ to SQL class.
this.Repository.InsertNote(CurrentNote);
}
this.Repository.Save();
}
catch (ApplicationException ex)
{
MessageBox.Show(String.Format("{0}" + Environment.NewLine + "{1}", ex.Message, this.CurrentNote.GetRuleViolations().FirstOrDefault().ErrorMessage));
}
}
And for context here are the InsertNote()
and Save()
methods on my repository implementation (nothing special, just basic LINQ to SQL calls). 对于上下文,这里是我的存储库实现中的
InsertNote()
和Save()
方法(没什么特别的,只是基本的LINQ to SQL调用)。
public void InsertNote(UnvoucheredInventoryNote note)
{
_db.UnvoucheredInventoryNotes.InsertOnSubmit(note);
}
public void Save()
{
_db.SubmitChanges();
}
I have tried catching and re-throwing the exception in the Save()
method, but it still fails and points the throw
line in my OnValidate
method. 我曾尝试在
Save()
方法中捕获并重新抛出异常,但是它仍然失败,并在我的OnValidate
方法中指向了throw
线。
What would cause this exception to halt at that line and not bubble up to my catch
block in the Presentation layer? 是什么导致该异常在该行停止并且没有冒泡到Presentation层中的
catch
块?
Thanks to the comments from Reed Copsey and Astander, I figured it out. 感谢Reed Copsey和Astander的评论,我终于找到了答案。 My debugger was set to break on User Unhandled Exceptions for
ApplicationException
and System.Exception
. 我的调试器被设置为针对
ApplicationException
和System.Exception
用户未处理异常中断。
The reason I did not catch this earlier is because I thought that "handling" them in a catch block in some way would override the first chance functionality, but apparently this is not the case. 我之所以没有赶上这一点的原因是因为我认为以某种方式在catch块中“处理”它们会覆盖第一次机会功能,但显然并非如此。
Turning off the Break on Exception functionality allowed the Exception to bubble up to where I wanted it. 关闭“异常中断”功能可使异常气泡上升到我想要的位置。
For Brevity here were the steps performed to do this: 为了简洁起见,以下是执行此操作的步骤:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.