Exception Translation: CLR catches an exception and rethrows a different exception. The inner exception contains the original exception. : throw « Language Basics « C# / C Sharp






Exception Translation: CLR catches an exception and rethrows a different exception. The inner exception contains the original exception.

 

using System;
using System.Reflection;


public class MyClass {
    public static void MethodA() {
        Console.WriteLine("MyClass.MethodA");
        throw new Exception("MethodA exception");
    }
}

public class Starter {
    public static void Main() {
        try {
            Type zType = typeof(MyClass);
            MethodInfo method = zType.GetMethod("MethodA");
            method.Invoke(null, null);
        } catch (Exception except) {
            Console.WriteLine(except.Message);
            Console.WriteLine("original: " + except.InnerException.Message);
        }
    }
}

 








Related examples in the same category

1.Throw and catch Exception
2.Throw exception from getter