User-Defined Exception Classes : Custom Exception « Language Basics « C# / CSharp Tutorial






using System;

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"));
    }

}
CountIsZeroException: CountIsZeroException: Zero count in DoAverage
   at MainClass.DoAverage()
   at MainClass.Main()








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.