title: Create a custom exception using C#
body: <p>I am trying to override the System.Exception class with my own exception class. I would like to add the parameter "code".</p>
  
  <p>I would want to do this:</p>
  
  <pre><code>throw new MyException(code, "message");
  </code></pre>
  
  <p>Then catch the exception using something like this:</p>
  
  <pre><code>catch (MyException e) {
      Console.WriteLine(e.code)
  }
  </code></pre>
  
  <p><strong>Update:</strong> The problem was that I needed to catch the exception as "(MyException e)" instead of "(Exception e)"</p>
  
  <p>This is my code so far:</p>
  
  <pre><code>public class MyException : System.Exception
  {
      public String ErrorCode = "";
  
      public MyException() : base()
      {
      }
  
      public MyException(string message, string code) : base(message)
      {
          this.ErrorCode = code;
      }
      public MyException(string message, Exception inner, string code) : base(message, inner)
      {
          this.ErrorCode = code;
      }
  }
  </code></pre>
  
  <p>This gives me an error that "System.Exception does not contain a definition for 'ErrorCode'...". What am I doing wrong?</p>