tags:

views:

107

answers:

3

One of the methods that I'm creating throws an exception. What's the clearest way of showing (either in code or comments) that my method could throw an exception and therefore a try{} and catch{} needs to be applied to my method.

Thanks!

+23  A: 
Andrew Hare
+1 Nice answer providing link to documentation, example and even a screenshot. :)
Luiz Damim
+1  A: 

In all of the MSDN documentation, every method shows what it may throw. I like this idea and thus in my comments I do something like:

// throws: MyDangerousError, StupidProgrammerError

If you want to go into more detail you can explain in what situations each error is thrown, often though the error name is enough to give users an Idea.

DeusAduro
Please see Andrew Hare's answer to learn how to do it right. The users won't see your comments, unless you want them to have to look at your source for every new method!
Alexey Romanov
+1  A: 

Sadly, clarity isn't the only issue. Otherwise, you could do this:

public void Method_MayThrowException() {
  ..
}

Since that is undesirable for other reasons, a comment that can be picked up by intellisense is likely to work the best.

Also, if you're open to add-ons or process modifications, you can read about Spec#. Or you could implement FxCop rules.

John Fisher