Search code examples
c#constructorcustom-exceptions

Custom Exception: customize message before call the base constructor


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 ?


Solution

  • 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) 
        {        
        }
    }