简体   繁体   English

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

[英]designing exceptions for custom dll used by third party web sites

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. 我在决定如何从此dll引发异常方面遇到麻烦,该dll主要是自定义db系统的包装。

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. 该dll最终将由面向公众的网站使用。 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? 如果发生异常事件,我是否会在错误消息中抛出常规的.net异常?

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. 我使用了许多第三方dll,这些dll无法为您提供清晰的错误处理方式,我也不想成为其中的一个。

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. 如果调用方将对待您的异常与说InvalidOperationException完全相同,则应改为抛出InvalidOperationException

See Choosing the Right Type of Exception to Throw . 请参见选择正确的异常类型以引发

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. 您应该抛出自定义异常,即您的DLL比所捕获的异常了解更多有关错误的信息,如果您不知道该错误,那么您就不知道更多有关该错误,只需将其传递出去即可。

Just to be clear, this doesn't mean you can't throw your own exceptions on detecting errors. 只是要清楚一点,这并不意味着您不能在检测错误时抛出自己的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM