using System; using System.Linq; namespace GenericConsoleApp { public class GenericException : Exception { public GenericException(string message) : base(message) { } } public class GenericException : GenericException { private T mValue; public GenericException(string message, T value) : base(message) { mValue = value; } public T Value { get { return mValue; } } } class Program { static void Main(string[] args) { HandleException(); HandleException(); HandleException(); HandleException(); Console.ReadKey(); } static void HandleException() where T : new() { try { ThrowException(); } catch (GenericException ex) { Type exceptionType = ex.GetType(); if (exceptionType.IsGenericType) { // Find the type of the generic parameter Type genericType = ex.GetType().GetGenericArguments().FirstOrDefault(); if (genericType != null) { Console.WriteLine("Caught exception of type '{0}'.", genericType.FullName); } } } catch (Exception) { Console.WriteLine("Failed to handle specific exception."); } } static void ThrowException() where T : new() { throw new GenericException( string.Format("Exception of type '{0}' has been thrown.", typeof(T).FullName), new T() ); } } }