6

I have generic exception class like this:

public class DuplicateException<TEntity> : Exception
{
    public TEntity Entity { get; set; }
}

And I have non-generic method which might throw constructed generic exception:

void Save()
{
    throw new DuplicateException<SomeEntity>();
}

This method may throw this generic exception but only of this one constructed type DuplicateException<SomeEntity> and it cannot throw this exception with some other type parameter instead of SomeEntity.

Now I want to specify this fact in xml-comment for Save method. This article describes a little bit how to comment methods with generic exception and I've tried these two alternatives:

1) Inserts by defauly by autocomplete in VS:

/// <exception cref="DuplicateException{TEntity}" />

2) Replaced TEntity with SomeEntity

/// <exception cref="DuplicateException{SomeEntity}" />

But in both cases output XML still states that this method might throw generic non-constructed type which doesn't mention SomeEntity at all:

<exception cref="T:MyNameSpace.DuplicateException`1" />
Snowbear
  • 16,924
  • 3
  • 43
  • 67
  • Is it necessary to throw a generic exception? The only use of that would be if a particular code fragment could potentially throw DuplicateException and DuplicateException, and you only want to catch one but not the other. – David Nelson Apr 01 '12 at 03:43
  • Ok, but why not just have a single DuplicateException that could be thrown from any Save* method? Why does it need to be generic? – David Nelson Apr 01 '12 at 19:14
  • Being strongly typed doesn't really get you much. Yes, you can have a strongly typed property with the object that was being saved. But what are you going to do with that strongly typed property? I think you are going to be better off with a non-generic exception here. – David Nelson Apr 02 '12 at 05:51

1 Answers1

3

The purpose of the cref attribute is to link to the documentation for a type. But there is no documentation for concrete generic types, so it's not surprising that the generated cref attribute is for the generic type definition. Your concern is that you want to display something different that what is in the link. You can do that when using the element, because the content of the element is the text of the link. But in the element, the content of the element is the description of when the exception occurs. So I don't think there is a way to do what you are looking for.

David Nelson
  • 3,666
  • 1
  • 21
  • 24