0

I'm producing a prototype system that will need to throw well-explained exceptions in order to make it a bit more user-friendly. The thing is, being as this system is undergoing development, I don't want to spend a lot of time creating exception classes until I've implemented the system and I know it works! Therefore, I'm thinking along the lines of creating a sort of "dummy" exception class that shows a compiler warning wherever it's thrown, reminding me to replace it with a more meaningful exception later.

public sealed class DummyException
{
    public DummyException() : base()
    {
        // How do I generate a compiler warning which will display once for each
        // instance that this code is called?
    }
}

I know I can use a TODO comment in each instance, but I'd rather use my proposed method so that I'll always get the warning whenever I use the dummy exception (rather than relying on me to remember to put a "TODO" comment on every thrown exception). Is this possible, and if so, how do I do it?

Lee.J.Baxter
  • 495
  • 4
  • 12
  • 2
    Look for "custom compiler warning": http://stackoverflow.com/questions/154109/custom-compiler-warnings http://stackoverflow.com/questions/968249/c-create-custom-warning-in-visual-studio-if-certain-method-is-used-in-source-c – Ian Jan 21 '16 at 12:38
  • 2
    You *could* use Roslyn but why don't you just throw `DummyException` and then replace all `DummyExceptions` later? – Jeroen Vannevel Jan 21 '16 at 12:38
  • @JeroenVannevel Good point! I never thought of using "Find" to see where I'm throwing a "DummyException"! Please could you post this and I'll mark it as the answer! – Lee.J.Baxter Jan 21 '16 at 12:43
  • Find works for `ToDo` as well. I myself *mark* unfinished or to-be-checked-later parts with special comment `//!` and then search for it prior building release version. – Sinatr Jan 21 '16 at 12:44
  • You could use the [Obsolete] attribute. Do beware that the approach is questionable, you add exception handling only after you discovered it is necessary *and* you figured out how to actually handle the specific mishap you discovered while testing. – Hans Passant Jan 21 '16 at 12:45
  • @Sinatr "TODO" is good, but if I threw a "DummyException" and forgot to mark it with "TODO", I'd have the odd stray "DummyException" kicking around! – Lee.J.Baxter Jan 21 '16 at 12:47
  • At best, this question is too broad. It is to some extent a duplicate of the marked duplicate question, though that question is old enough that it doesn't discuss Roslyn features as a possible work-around. It does however mention other possibilities, like `[Obsolete]` and the `message` compiler directive. Please do some research, figure what _specific_ approach you want to use, and if you have trouble at that point, post a question with the _specific_ details pertaining to your attempt to solve the problem. – Peter Duniho Jan 22 '16 at 00:59

0 Answers0