4

I'd like to create a custom exception with a constructor taking some context and building its message itself. In Java, that would look like (see how to create custom exception with a number in constructor in Java)

public MyException(String message, IContext context){
    super(message + " " + context.someData);
}

In C# however, the compiler tells me that base() is not valid in this context.

public MyException(IContext context)
{
    base(String.Format("Context is: {0}", context));
}

What's wrong?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
PPC
  • 1,734
  • 1
  • 21
  • 41

1 Answers1

3

The C# syntax is:

public MyException(IContext context)
    : base(String.Format("Context is: {0}", context))
{
}
Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158