简体   繁体   English

没有抛出ApplicationException来正确捕获块

[英]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周围的代码:( RepositoryIRepository的注入实现)。

    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 . 我的调试器被设置为针对ApplicationExceptionSystem.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: 为了简洁起见,以下是执行此操作的步骤:

  1. Go to the Debug menu in VS 转到VS中的“调试”菜单
  2. Click on 'Exceptions...' 点击“例外...”
  3. Expand 'Common Language Runtime (CLR) Exceptions 展开“公共语言运行时(CLR)异常
  4. Uncheck the type of Exception you DO NOT want to be caught by the First Chance debugger 取消选中您希望被First Chance调试器捕获的异常类型
  5. Click OK 点击确定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM