Вопрос

I have a standalone application that does data checking of flat files before they are imported into a SQL database.

In a context like this, does it make sense to throw a System.Data.ConstraintException? (The example is contrived.)

if(_keys.ContainsKey(key))
{
    throw new ConstraintException(string.Format("Uniqueness violated! " + 
        "This unique combination of '{0}' already found on line {1}", 
        GetUniquenessColumnList(), _keys[key] ));
}

ConstraintException's documentation states that it "represents the exception that is thrown when attempting an action that violates a constraint."

Is there a problem with using the built-in exception this way? Is there a better one to use? Should I build my own, even though this exception seems tailor-made to my situation?

Это было полезно?

Решение

I think you've answered your own question

...this exception seems tailor-made to my situation?

Why reinvent the wheel?

EDIT:

Check out this MSDN article: Choosing the Right Type of Exception to Throw

Consider throwing existing exceptions residing in the System namespaces instead of creating custom exception types.

...

Do not create and throw new exceptions just to have your team's exception.

Другие советы

No, there's no problem with doing that. No need to create a new exception if a suitable one already exists.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top