1

I defined this custom exception

public class ThirdPartyServiceException : System.Exception
{
    public ThirdPartyServiceException(string message, Exception innerException): base(message, innerException)
    { }
}

That's fine, but now I want customize the exception message before call the base constructor. Something like:

public class ThirdPartyServiceException : System.Exception
{
    public ThirdPartyServiceException(string code, string message, string description, Exception innerException)
    {
        base(string.format("Error: {0} - {1} | {2}", code, message, description), innerException) 
    }
}

But I can't call the base constructor in this way. So how I can do ?

DevT
  • 1,411
  • 3
  • 16
  • 32
  • possible duplicate of [Calling the base constructor in C#](http://stackoverflow.com/questions/12051/calling-the-base-constructor-in-c-sharp) – ASh Jun 09 '15 at 09:23

2 Answers2

2

Override the Message property.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • @MathieuF - I disagree, I think this is the best way to customise the exception message: insisting that it be done by passing to the base constructor is a classic example of the XY problem. – Joe Jun 09 '15 at 09:49
1
public class ThirdPartyServiceException : System.Exception
{
    public ThirdPartyServiceException(string code, string message, string description, Exception innerException)
    :base(string.format("Error: {0} - {1} | {2}", code, message, description), innerException) 
    {        
    }
}
ASh
  • 34,632
  • 9
  • 60
  • 82