Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attribute to inform method caller of the type of exceptions thrown by that method

I'm not looking to implement the Java "throws" keyword. See http://www.artima.com/intv/handcuffsP.html for a discussion on the merits of the throws keyword and why it was not implemented in C#.

I am, however, curious if there's a way to create an attribute like the following:

[ThrowsException( exceptionType = NullReferenceException )] 
[ThrowsException( exceptionType = AuthenticationException )]
public void Login( Credentials credz ) 
{
    // ... etc...
}

such that - when calling a method which has been decorated with one or multiple ThrowsException attributes, the type of exceptions thrown by said method (at least the ones that are explicitly declared by the ThrowsException attribute) would be visible in the method's documentation

This is not the same as the Java "throws" keyword as it would not require that the caller handle these exceptions. Doing so could introduce breaking changes e.g. in a client application that does not handle new exceptions introduced by a version change.

While one could use:

/// <exception cref="member">description</exception>

My intent for using attributes is so that the project does not compile if the name of the exception has been changed or if the exception no longer exists. Therefore, How To Document Thrown Exceptions, is not the same question.


Update: 2013-05-23

I've figured out a way to resolve via the use of an attribute and without the use of plug-ins. I will try to get around to it this weekend and will be happy to post the solution if it works as expected. If someone beats me to posting a solution, I will happily accept their answer.

Since I won't be able to get around to this until Monday, I'm offering a bounty if you can beat me to it - An acceptable answer would:

  • ( not include the use of a visual studio plug-in or any third-party tool
  • && provide a way to include the exception(s) in the XML documentation
  • && ensure type safety is enforced during compilation )
  • || prove it is not possible to meet the preceding three requirements in order to provide a solution to the problem posed in this question

I would consider it acceptable for the XML documentation not to reflect the exceptions, from the ThrowsException attributes, until after the project has been built.

It would be interesting to see a Resharper-based solution (since it is common to most of the development shops I have worked in), but it will not be accepted if there is a solution that remains agnostic of third-party tools. Similarly, a solution that works only in Visual Studio would be accepted over a solution dependent on Resharper, but it would not be accepted if there is a solution that would work in other IDEs e.g. MonoDevelop (more frameworks supported - even better).

like image 338
Jordan Avatar asked May 22 '13 19:05

Jordan


People also ask

When an exception is thrown by a method What happens next?

When a method declares that it throws an exception, it is not required to handle the exception. The caller of a method that throws exceptions is required to handle the exceptions (or throw them to its caller and so on) so that the flow of the program can be maintained.

How do you call a method that throws an exception in Java?

The throw keyword in Java is used to explicitly throw an exception from a method or any block of code. We can throw either checked or unchecked exception. The throw keyword is mainly used to throw custom exceptions.

What does it mean when an exception is thrown?

The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.

How do you throw a new exception?

Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception includes a message which is a human-readable error description. It can often be related to problems with user input, server, backend, etc.


2 Answers

In C# you document classes and their members with a XML documentation. In Visual Studio, start typing slashes over something and by the third slash it'll auto-generate the most common tags for you to fill in. It looks vaguely like Javadoc and JSDoc.

You are looking, specifically, for this tag.

like image 155
Geeky Guy Avatar answered Oct 19 '22 14:10

Geeky Guy


My intent for using attributes is so that the project does not compile if the name of the exception has been changed or if the exception no longer exists. Therefore, How To Document Thrown Exceptions, is NOT the same question.

You can do this without using attributes, instead by writing <exception> documentation and configuring your project like this:

Project properties -> 'Build' tab:
'Output' section: Check 'XML documentation file'.
'Treat warnings as erros' section: Check 'Specific warnings' and add warning 1574.

This way project won't compile if XML documentation cref attribute value cannot be resolved.

like image 43
Stipo Avatar answered Oct 19 '22 14:10

Stipo