Extends Exception : Custom Exception « Language Basics « C# / CSharp Tutorial






using System;
using System.Runtime.Serialization;

[Serializable]
public sealed class MyException : Exception
{
    private string stringInfo;
    private bool booleanInfo;

    public MyException() : base() { }
    public MyException(string message) : base(message) { }
    public MyException(string message, Exception inner): base(message, inner) { }

    private MyException(SerializationInfo info, StreamingContext context) : base(info, context)
    {
        stringInfo = info.GetString("StringInfo");
        booleanInfo = info.GetBoolean("BooleanInfo");
    }

    public MyException(string message, string stringInfo, bool booleanInfo) : this(message)
    {
        this.stringInfo = stringInfo;
        this.booleanInfo = booleanInfo;
    }

    public MyException(string message, Exception inner, string stringInfo, bool booleanInfo): this(message, inner)
    {
        this.stringInfo = stringInfo;
        this.booleanInfo = booleanInfo;
    }

    public string StringInfo
    {
        get { return stringInfo; }
    }

    public bool BooleanInfo
    {
        get { return booleanInfo; }
    }

    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("StringInfo", stringInfo);
        info.AddValue("BooleanInfo", booleanInfo);

        base.GetObjectData(info, context);
    }

    public override string Message
    {
        get
        {
            string message = base.Message;
            if (stringInfo != null)
            {
                message += Environment.NewLine + stringInfo + " = " + booleanInfo;
            }
            return message;
        }
    }
}

public class MainClass
{
    public static void Main()
    {
        try
        {
            throw new MyException("Some error", "SomeCustomMessage", true);
        } 
        catch (MyException ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.BooleanInfo);
            Console.WriteLine(ex.StringInfo);
        }
    }
}
Some error
SomeCustomMessage = True
True
SomeCustomMessage








1.24.Custom Exception
1.24.1.User-Defined Exception Classes
1.24.2.Use a custom Exception
1.24.3.Derived exceptions must appear before base class exceptions.
1.24.4.A custom exception with HelpLink and Source
1.24.5.Extends Exception
1.24.6.Create your own exception class based on Exception
1.24.7.CustomException is an application exception that supports remoting.