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?