Throw and catch Exception : throw « Language Basics « C# / C Sharp






Throw and catch Exception

 

using System;

class ExceptionThrower {
    static void MethodOne() {
        try {
            MethodTwo();
        } finally { }
    }

    static void MethodTwo() {
        throw new Exception("Exception Thrown in Method Two");
    }

    public static void Main(String[] args) {
        try {
            ExceptionThrower FooBar = new ExceptionThrower();
            MethodOne();
        } catch (Exception e) {
            Console.WriteLine(e.Message);
        } finally {
            // Cleanup code
        }
    }
}

 








Related examples in the same category

1.Exception Translation: CLR catches an exception and rethrows a different exception. The inner exception contains the original exception.
2.Throw exception from getter