Egyedi kivételkezelés 3.

Filed Under (Uncategorized) by nameless on 02-08-2009


Ha valóban megfelelő egyedi kivételosztályt szeretnénk építeni, akkor meg kell bizonyosodnunk róla, hogy a típus megfelel a kivételközpontú .NET legjobb gyakorlatának. Pontosabban, ehhez az kell, hogy az egyedi kivételünk

- az Exception/ApplicationException típusból származzon
- a [System.Serializable] attribútum jelölje meg
- definiáljon egy alapértelmezett konstruktort
- definiáljon egy olyan konstruktort, amely beállítja az örökölt Message tulajdonságot
- definiáljon egy konstruktort a belső kivételek kezelésére
- definiáljon egy konstruktort a típusok sorosításának kezelésére.

CarIsDeadException osztály:

 [Serializable]
  public class CarIsDeadException : ApplicationException
  {
 
    public CarIsDeadException() { }
    public CarIsDeadException(string message) : base(message) { }
    public CarIsDeadException(string message, Exception inner) : base(message, inner) { }
    protected CarIsDeadException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
      : base(info, context) { }
 
 
    public CarIsDeadException(string message, string cause, DateTime time)
      : base(message)
    {
      causeOfError = cause;
      errorTimeStamp = time;
    }
 
    private DateTime errorTimeStamp;
    private string causeOfError;
 
    public DateTime TimeStamp
    {
      get { return errorTimeStamp; }
      set { errorTimeStamp = value; }
    }
    public string Cause
    {
      get { return causeOfError; }
      set { causeOfError = value; }
    }
  }

Post a comment