September 20, 2012

Code Repository - Custom Exception

//------------------------------------------------------------------------------
// Created by: Pete
// Created on: Sep 10, 2012
//------------------------------------------------------------------------------

using System;
using System.Runtime.Serialization;

namespace AdventureWorks.DataAccess
{
    /// <summary>
    /// An exception thrown when errors occur in the repository class
    /// </summary>
    [Serializable]
    public class RepositoryException : Exception
    {
        public RepositoryException()
        {
        }

        public RepositoryException(string message)
            : base(message)
        {
        }

        public RepositoryException(string message, Exception innerException)
            : base(message, innerException)
        {
        }

        protected RepositoryException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }
    }
}

備註
  1. [Serializable]這個attribute必須加入,否則無法序列化
  2. Protected RepositoryException(SerializationInfo info, StreamingContext context) : base(info, context)必須加入,否則無法反序列化
  3. 如果有自訂property於例外類別中,則必須額外再實作ISerializable的public void GetObjectData(SerializationInfo info, StreamingContext context)方法

No comments: