gpt4 book ai didi

c# - 使用现有框架 ConstraintException 解决数据约束违规问题

转载 作者:行者123 更新时间:2023-12-02 05:37:04 24 4
gpt4 key购买 nike

我有一个独立的应用程序,它在将平面文件导入 SQL 数据库之前对其进行数据检查。

在这样的上下文中,抛出 System.Data.ConstraintException 是否有意义? (这个例子是人为的。)

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声明它“表示在尝试违反约束的操作时抛出的异常。”

这样使用内置异常有问题吗?有没有更好用的?我是否应该构建自己的,即使这个异常(exception)似乎是为我的情况量身定做的?

最佳答案

我想你已经回答了你自己的问题

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

为什么要重新发明轮子?

编辑:

查看这篇 MSDN 文章:Choosing the Right Type of Exception to Throw

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

...

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

关于c# - 使用现有框架 ConstraintException 解决数据约束违规问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11532108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com