为第三方网站使用的自定义 dll 设计例外

发布于 2024-12-08 23:30:59 字数 341 浏览 0 评论 0原文

我正在设计一个类库,该类库将发送给公众以供他们的应用程序使用。它与他们需要安装在服务器上的自定义数据库系统进行交互。

我无法决定如何从这个 dll 中抛出异常,该 dll 主要是自定义数据库系统的包装器。

我假设我必须创建可以指示各种类型错误的自定义异常,包括验证、数据完整性错误等。

该 dll 最终将由面向公众的网站使用。我想通过抛出适当的异常来向最终用户提供良好的反馈,以便网站可以决定是否显示它们。但我不确定是否可以预见所有用户输入错误。如果发生无异常事件,我是否只抛出常规 .net 异常并显示错误消息?

我使用过许多第三方 dll,但它们没有给你一个明确的方法来处理错误,我不想成为其中之一。

I'm designing a class library that will be sent out to the public for use with their applications. It interacts with a custom db system that they need to install on their servers.

I'm having trouble deciding how to throw exceptions from this dll which is mainly a wrapper to the custom db system.

I assume I have to create custom exceptions that can indicate various types of errors, including validations, data integrity errors etc.

This dll will be eventually used by public facing web sites. I would like to provide a good feedback to the end user by throwing proper exceptions so that the site can make a decision to display them or not. But I'm not sure if it is possible to anticipate all the user input errors. In case of an unexcepted event, do I just throw a regular .net exception with the error message?

I have used many third party dlls that do not give you a clear way to handle the errors and I don't want to be one of them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪若未夕 2024-12-15 23:30:59

创建自定义异常的唯一原因是调用代码是否需要显式捕获它以了解您的异常与其他异常之间的区别。

如果调用者要以与 InvalidOperationException 完全相同的方式处理异常,例如 InvalidOperationException,那么您应该抛出 InvalidOperationException。

请参阅选择正确的异常类型

The only reason to creaqte a custom exception is if the calling code needs to catch it explicitly to know the difference between your exception and some other exception.

If the caller is going to treat your exception exactly the same as, say, InvalidOperationException, then you should throw InvalidOperationException instead.

See Choosing the Right Type of Exception to Throw.

小傻瓜 2024-12-15 23:30:59

您应该重新抛出自定义异常,其中您的 DLL 对错误的了解比您捕获的异常中指示的更多,如果您不了解它,那么您就不了解更多信息,只需传递它即可。

需要明确的是,这并不意味着您不能在检测错误时引发自己的异常。

You should rethrow custom exceptions where your DLL knows more about the error than is indicated in the exception you are catching, if you don't know about it, then you don't know MORE about it, just pass it along.

Just to be clear, this doesn't mean you can't throw your own exceptions on detecting errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文