CSharp examples for Custom Type:Exception
Implement a Custom Exception Class
using System;//from www . j av a 2 s. com using System.Runtime.Serialization; [Serializable] public sealed class CustomException : Exception { private string stringInfo; private bool booleanInfo; public CustomException() : base() { } public CustomException(string message) : base(message) { } public CustomException(string message, Exception inner) : base(message, inner) { } private CustomException(SerializationInfo info, StreamingContext context) : base(info, context) { stringInfo = info.GetString("StringInfo"); booleanInfo = info.GetBoolean("BooleanInfo"); } public CustomException(string message, string stringInfo, bool booleanInfo) : this(message) { this.stringInfo = stringInfo; this.booleanInfo = booleanInfo; } public CustomException(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; } } } class MainClass { public static void Main() { try { throw new CustomException("Some error", "SomeCustomMessage", true); } catch (CustomException ex) { Console.WriteLine(ex.Message); } } }