ValidationException has changed

Topics: Developer Forum
Apr 23, 2007 at 8:36 AM
Hi Simon,

I have just updated my project to reference the latest source code of the ValidationFramework, and now have found it won't compile due to not knowing what ValidationException is.

My code within a try catch had an if statement which output the error message to the screen if the exception was a ValidationException:

if (ex is ValidationFramework.ValidationException)
{
// do stuff
}

I can't seem to find out what the equivalent is now - could you please let me know?

Thanks,

Dan
Apr 23, 2007 at 10:18 AM
...actually in my Validate Method within my base class I have the following code:

/// <summary>
/// Uses the Validation Framework to validate the record against it's defined validation rules
/// </summary>
/// <param name="record"></param>
public virtual void ValidateRecord(ListRecordBase record)
{
// Validate the Record
#region Validation

Interfaces.IListRecordValidatable listRecord = record;
if (!listRecord.Validate())
{
StringBuilder sb = new StringBuilder();

foreach (ValidationResult r in listRecord.ValidatorResultsInError)
{
sb.AppendLine(String.Format(CultureInfo.InvariantCulture, "{0} ({1})", r.ErrorMessage, r.Rule.RuleInterpretation));
}

throw new ValidationException(sb.ToString());
}

#endregion
}

...i could create my own ValidationException object, but wondered whether you have one i could already use?

Cheers,

Dan
Coordinator
Apr 23, 2007 at 12:23 PM
Edited Apr 23, 2007 at 12:24 PM
A little background first. Have a read of this http://blogs.msdn.com/kcwalina/archive/2007/01/30/ExceptionHierarchies.aspx

The reason I removed the ValidationException was because, after some detailed analysis, I could not see a scenario where it would be useful to the consumers of the framework.

I assume that you are throwing the ValidationException from your persistence layer before you save it back to a database/file. If this is the case I would say that it falls into the category of “Usage Error”. The reason being twofold
  1. If a frontend is calling your code the frontend should not have attempted to persist that invalid object. It can easily check an IsValid flag and then show the use the appropriate error message to the user.
  2. If you are writing a business layer it is unlikely that the consumer of your code would have a reason to catch a ValidationException. It is more likely that they are in error on how they are using your business layer. If this is the case throwing an InValidOperationException should suffice. They should discover their error at development time and there will be on reason to ever catch that exception. This is similar to the fact that you rarely catch ArgumentExceptions.

I have made a few assumptions about your code here. Feel free to post up more detail about you exact scenario.

Regards

Simon
Coordinator
Apr 23, 2007 at 12:40 PM
BTW

I am also planning on writing some exception guidelines in relation to this project. Should be completed within the next few days. They will be included in the chm.
Apr 23, 2007 at 12:46 PM
Hi Simon,

Yes you are right - in my case it is a custom details view, which has an OnItemInserted event, which in the event args passed in contains an exception object if an insert error occurred. I used this to check for a validation error (there isn't a BeforeInsert event), which I show the the UI front end, as opposed to bubble up other unknown exceptions. There is no reason for me not to use my own exception object for this though.

Thanks,

Dan