User-Defined Exception Classes

In this chapter you will learn:

  1. User-Defined Exception Classes
  2. Exception properties
  3. extends Exception to creat your own exception class

User-Defined Exception Classes

using System;/*from  j  a  v a 2 s .  c o  m*/

public class CountIsZeroException: ApplicationException{
    public CountIsZeroException(){
    }
    public CountIsZeroException(string message) : base(message)
    {
    }
    public CountIsZeroException(string message, Exception inner) : base(message, inner)
    {
    }
}

class MainClass{
    public static void Main() {
        try {
            DoAverage();
        }
        catch (CountIsZeroException e)
        {
            Console.WriteLine("CountIsZeroException: {0}", e);
        }
    }
    public static void DoAverage() {
        throw(new CountIsZeroException("Zero count in DoAverage"));
    }

}

The code above generates the following result.

Exception properties

A custom exception with HelpLink and Source.

using System;//  jav  a  2 s . c om

public class CustomException : ApplicationException
{
  public CustomException(string Message) : base(Message)
  {
    this.HelpLink = "See the Readme.txt file";
    this.Source = "My Program";
  }
}

class MainClass
{
  public static void Main()
  {
    try
    {
      Console.WriteLine("Throwing a new CustomException object");
      throw new CustomException("My CustomException message");
    }
    catch (CustomException e)
    {
      Console.WriteLine("HelpLink = " + e.HelpLink);
      Console.WriteLine("Message = " + e.Message);
      Console.WriteLine("Source = " + e.Source);
      Console.WriteLine("StackTrace = " + e.StackTrace);
      Console.WriteLine("TargetSite = " + e.TargetSite);
    }
  }
}

The code above generates the following result.

extends Exception to creat your own exception class

using System;//  ja va2s  .  c  om
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);
        }
    }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to use HashSet
  2. A set of integer
Home » C# Tutorial » Exception
Exception
Exception classes
try...catch
catch exceptions
finally
throw exception
System exception
User-Defined Exception Classes