# Thursday, June 16, 2005

For some reason I always have to go searching for this sample code when I want it and it's never as easy to find as I want it to be.  This is a very nice starting point for creating your own custom exception classes in C#.  The translation to VB.NET is straightforward.

using System;
using System.Runtime.Serialization;

namespace YourNamespaceHere
{
    /// <summary>
    /// YourCustomException is a vanilla custom exception for this application.
    /// </summary>
    [Serializable()]
    public class YourCustomException : Exception, ISerializable
    {
        /// <summary>
       
/// Calls the default exception constructor.
       
/// </summary>
       
public YourCustomException()
        {
        }
       
/// <summary>
       
/// Calls the default exception constructor with a message parameter.
       
/// </summary>
       
/// <param name="message">The exception message.</param>
       
public YourCustomException(string message) : base(message)
        {
        }
       
/// <summary>
       
/// Calls the default exception constructor with a message and innerException parameter.
       
/// </summary>
       
/// <param name="message">The exception message.</param>
       
/// <param name="inner">The inner exception.</param>
       
public YourCustomException(string message, Exception inner): base(message, inner)
        {
        }
       
/// <summary>
       
/// Calls the default exception constructor with a serialization info and streaming context parameter.
       
/// </summary>
       
/// <param name="info">The SerializationInfo that holds the serialized object data about the exception being thrown.</param>
       
/// <param name="context">The StreamingContext that contains contextual information about the source or destination.</param>
       
public YourCustomException(SerializationInfo info, StreamingContext context) : base( info, context )
        {
        }
    }
}

Note that there is some controversy regarding whether you are supposed to inherit from Exception or ApplicationException for custom exceptions due to Microsoft giving inconsistent guidance.

This is the source of information I've giving the most authority to on this issue in my choice of inheriting from Exception:

Brad Abrams Blog

Thursday, June 16, 2005 11:10:04 AM (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  |  Related posts:
On the road to better exception handling
Fail Fast