Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify exceptions to be thrown by an implementor of an interface?

I'm currently developing a solution and have designed it in a way such that it strongly implements the strategy/provider pattern. As such the solution exposes a number of interfaces and contains default implementations of these interfaces which can be replaced via a DI type methodology.

Where the host application uses a number of these interfaces it is expecting to handle certain exceptions that may occur, for example IDataRetriever interface has a method SomeDataType GetData(int timeout);and the host can handle some custom exceptions such as DataRetrievalTimeoutException or NetworkConnectionException.

My question is, what is the best way to mark up the interface class such that when a developer implements it they would know that certain exceptions should be thrown and would be handled by the host?

At the moment I have just added the exception xml tags to the methods xml comment - does this suffice?

like image 441
gouldos Avatar asked Oct 13 '10 11:10

gouldos


1 Answers

The XML tags (and any other documentation you want to write) are basically the closest you've got in "vanilla" .NET at the moment.

You might want to look at Code Contracts which lets you annotate your interface with contracts, which can include exceptions, preconditions etc.

like image 179
Jon Skeet Avatar answered Sep 27 '22 18:09

Jon Skeet