简体   繁体   中英

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.

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.

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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