Exemple #1
0
    private static bool GenericTryCatch <T>()
    {
        Exception thrown = new GenException <T>();

        try
        {
            throw thrown;
        }
        catch (GenException <T> )
        {
            return(true);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Caught {0} (expected {1})", ex.GetType(), thrown.GetType());
            return(false);
        }
    }
    public static bool ExceptionTest(bool throwException)
    {
        string ExceptionClass = typeof(GenException <T>).ToString();

        try
        {
            if (throwException)
            {
                throw new GenException <T>();
            }
            else
            {
                return(true);
            }
        }
        catch (Exception E)
        {
            string EText = E.ToString();
            // Ensure the type of the Exception --> this is currently broken
            if ((EText != null) && (EText.IndexOf(ExceptionClass) >= 0))
            {
                //Ensure we can correctly downcast the Exception to it's original type
                try
                {
                    GenException <T> tmp = (GenException <T>)E;
                    return(true);
                }
                catch
                {
                    Console.WriteLine("Failed to downcast Exception object for: " + typeof(Gen <T>));
                    return(false);
                }
            }
            Console.WriteLine("Failed to detect " + ExceptionClass + " in Exception class string");
            return(false);
        }
    }