2

I'm trying to write code to create an EpicFailException class as a subclass of ApplicationException. I have to add a constructor that defaults the HelpLink property to google.com. Below is what I have so far. Any suggestions for how to apply the subclass?

 public class ApplicationException : Exception
 {
     public ApplicationException(string auxMessage, Exception inner) :
         base()
     {
         this.HelpLink = "http://google.com";
     }
 }
casperOne
  • 73,706
  • 19
  • 184
  • 253
John
  • 21
  • 1

2 Answers2

1

The .NET class design guidelines have been changed; the current recommendation is to derive from Exception and not from ApplicationException.

That said, if you have the need to set the HelpLink property in a number of derived exceptions, you might want to create a base class which demands the value for the HelpLink property as a parameter, and then derive from that:

public abstract MyExceptionBase : Exception
{
    protected MyExceptionBase(string message, string helpLink) : 
        base(message)
    {
        HelpLink = helpLink;
    }
}

public MyException : MyExceptionBase
{
    public MyException(string message) : 
        base(message, "http://www.google.com")
    { }
}

Note that you should still follow all of the other guidelines for custom exceptions, such as serialization, parameterless constructor, etc.

Community
  • 1
  • 1
casperOne
  • 73,706
  • 19
  • 184
  • 253
  • I want to write the code to create an EpicFailException class as a subclass of ApplicationException. Then i'm trying to add a constructor that defaults the HelpLink property to "http://google.com". – John May 01 '11 at 22:05
  • @John: The above does what you want, and follows proper .NET design guidelines. See the links in the answer for more information. – casperOne May 01 '11 at 22:06
0

I'm not sure if I get the question, but you might be looking for

base(auxMessage,inner)

instead of just

base()
Achim
  • 15,415
  • 15
  • 80
  • 144