I am having an issue where I override the OnValidate
method of a LINQ to SQL class. 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:
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.
In my Form I have a button click event that calls the SubmitChanges
method on my repository class. 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.
Here is the code around the Button: ( Repository
is an injected implementation of an 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).
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.
What would cause this exception to halt at that line and not bubble up to my catch
block in the Presentation layer?
Thanks to the comments from Reed Copsey and Astander, I figured it out. My debugger was set to break on User Unhandled Exceptions for ApplicationException
and 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.
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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.