Create your own exception class based on Exception : Custom Exception « Language Basics « C# / CSharp Tutorial






using System;
using System.Collections;

public class MyException : Exception
{
    public MyException( String reason, Exception inner ) :base( reason, inner ) {
    }
}

public class MainClass
{
    static void Main() {
        try {
            try {
                ArrayList list = new ArrayList();
                list.Add( 1 );

                Console.WriteLine( "Item 10 = {0}", list[10] );
            }
            catch( ArgumentOutOfRangeException x ) {
                throw new MyException( "I'd rather throw this",x ) ;
            }
            finally {
                Console.WriteLine( "Cleaning up..." );
            }
        }
        catch( Exception x ) {
            Console.WriteLine( x );
            Console.WriteLine( "Done" );
        }
    }
}
Cleaning up...
MyException: I'd rather throw this ---> System.ArgumentOutOfRangeException: Index was out of range.
Must be non-negative and less than the size of the collection.
Parameter name: index
   at System.Collections.ArrayList.get_Item(Int32 index)
   at MainClass.Main()
   --- End of inner exception stack trace ---
   at MainClass.Main()
Done








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.