ALLINSIGHT

Home of the AlmostImplementedException

How to document / show exceptions a method can throw

It is very helpful to know if and what exceptions a method can throw. Unfortunately there is no throws-keyword like in Java.
But there is an easy way to document what exceptions can be thrown and show this information in intellisens. You can use the xml-documentation of a method to document which exceptions are thrown and why.

It is good practice to document your code like this, especially when others are supposed to use your methods without seeing the source code.

The syntax for the documentation is like this:

Important is only the first line, but i wrote the rest to put it in context. The method actually don’t have to throw the exception, neither it needs exist. The documentation doesn’t care what you do, so be careful to keep up to date which exceptions are thrown.

Intellisens is now showing something like this:

Intellisens showing an exception-description of a method in VisualStudio

Share :

, , ,

Leave a Reply

Your email address will not be published. Required fields are marked *