SqlException class does’nt have public constructor

I am currently preparing ado.net 3.5 course so I try to throw SqlException in one of my examples and realize that SqlException class is sealed and has no public constructor.

I have found solution on Rido blog http://blogs.msdn.com/rido how to create SqlException using reflection.

        public static SqlException CreateSqlException(string errorMessage, int errorNumber)

        {

 

            SqlErrorCollection collection = GetErrorCollection();

            SqlError error = GetError(errorNumber, errorMessage);

            MethodInfo addMethod = collection.GetType().

            GetMethod("Add", BindingFlags.NonPublic | BindingFlags.Instance);

            addMethod.Invoke(collection, new object[] { error });

            Type[] types = new Type[] { typeof(string), typeof(SqlErrorCollection) };

            object[] parameters = new object[] { errorMessage, collection };

            ConstructorInfo constructor = typeof(SqlException).

            GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);

            SqlException exception = (SqlException)constructor.Invoke(parameters);

            return exception;

        }

        private static SqlError GetError(int errorCode, string message)

        {

            object[] parameters = new object[] {

            errorCode, (byte)0, (byte)10, "server", message, "procedure", 0 };

            Type[] types = new Type[] {

            typeof(int), typeof(byte), typeof(byte), typeof(string), typeof(string),

            typeof(string), typeof(int) };

            ConstructorInfo constructor = typeof(SqlError).

             GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);

            SqlError error = (SqlError)constructor.Invoke(parameters);

            return error;

        }

 

        private static SqlErrorCollection GetErrorCollection()

        {

            ConstructorInfo constructor = typeof(SqlErrorCollection).

            GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { }, null);

            SqlErrorCollection collection = (SqlErrorCollection)constructor.Invoke(new object[] { });

            return collection;

        }

5 Comments on SqlException class does’nt have public constructor

  1. Miha Markic
    March 6, 2009 at 10:25 pm (15 years ago)

    Why would you need to throw a Sql(Server)Exception by yourself? To simulate some behaviour?

  2. Radenko Zec
    March 6, 2009 at 11:00 pm (15 years ago)

    Thanks for comment Miha

    I try to throw SqlException manually to show some thing about error handling for my Ado.net course.My need for throwing SqlException is only for testing purpose for this course.
    If we write some code to handle all types of SqlExceptions and show user friendly message if some error number occurs we could use this for testing that code?

  3. Miha Markic
    March 7, 2009 at 3:01 am (15 years ago)

    I see. Why don’t you create a real test scenario – I mean firing a bunch of SQL things and get real errors? But I guess you know why you are doing it that way πŸ™‚

  4. Sandy
    March 23, 2009 at 2:16 am (15 years ago)

    Hmmm graet I used it to test a deadlock scenario in my assignment…. I tried all sorts of things to get a real exception from SQL server but all got failed….

  5. Radenko Zec
    April 7, 2009 at 1:54 am (15 years ago)

    I am glad I can help πŸ™‚