Wrap Your Original Exception in C#

Exception handling mechanism is very essential in every application. A good coding stricture does not leave a single piece of code which might give trouble in application. That’s fine, C# has given facility to handle Exception using try-catch block.

Now as a developer we know there are thousand types of Exception which might occur in application and we know their technical meaning. But how one legitimate user will know their meaning? For example, there is one SMTP related Exception in your application due to down of the mail server. If we show the message something like that to user

“There is SMTP error and port 25 is not open” .

I am sure that from next day the user will never come back in your application because they don’t want to face those technical jargons in next time. Ha..Ha..Yes, this is the real scenario. So, the solution is, we have to wrap our original exception and we have to show a polite message to user like.

“We are unable to send your mail. Please try after some moment.”

User will think that, Oh, the application knows how to behave, at least. Cool, we have learned the necessity of Wrapping actual exception which is very much developer friendly and by wrap it with another exception we will make it user friendly.

At first we will implement one exception example without wrapping it with another exception object and have a look, how technical message it’s throwing to user interface part.

using System;

using System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks; 

namespace
ConsoleApplication1
{

   
public class TestClass

   
{

        public void ActualException()

        {

            try

            {

                throw new StackOverflowException();

            }

            catch (Exception ex)

            {

                throw;

            }

        }

        public void BubbledException()

        {

            try

            {

                ActualException();

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

   
}

   
class Program

   
{

        static void Main(string[] args)

        {

            try

            {

                TestClass t = new TestClass();

                t.BubbledException();

                Console.ReadLine();
 
            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

                Console.ReadLine();

            }
 
        }

   
}

}

Ok, It’s throwing “Stack overflow” exception but how many of our user does know the meaning of stack? If the user is not another developer.

Stack overflow exception

using System;

using System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks; 
namespace
ConsoleApplication1
{

   
public class TestClass

   
{

        public void ActualException()

        {

            try

            {

                throw new StackOverflowException();

            }

            catch (Exception ex)

            {

                throw;

            }

        }

        public void BubbledException()

        {

            try

            {

                ActualException();

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

   
}

   
class Program

   
{

        static void Main(string[] args)

        {

            try

            {

                TestClass t = new TestClass();

                t.BubbledException();

                Console.ReadLine();
 
            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message);

                Console.ReadLine();

            }
 
        }

   
}

Ok, It’s throwing “Stack overflow” exception but how many of our user does know the meaning of stack? If the user is not another developer.

Now, in this example we will wrap the actual exception with our custom exception class. We have developer one “customException” class which is derived from base “Exception” class. Within custom class we have implemented six constructors which will take different parameter flavor and those will help to manipulate different kind of wrapper object.

using System;

using System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.Text;
using
System.Threading.Tasks; 
namespace
ConsoleApplication1
{

   
[Serializable]

   
public class CustomException : Exception

   
{

        public CustomException()

            : base() { }
 
        public CustomException(string message)

            : base(message) { }
 
        public CustomException(string format, params object[] args)

            : base(string.Format(format, args)) { }
 
        public CustomException(string message, Exception innerException)

            : base(message, innerException) { }
 

        public CustomException(string forma
, Exception innerException, params object[] args)


            : base(string.Format(format, args), innerException) { }
 
        protected CustomException(SerializationInfo info,
StreamingContext context)

            : base(info, context) { }

   
}
 
   
public class TestClass

   
{

        public void ActualException()

        {

            try

            {

                throw new StackOverflowException();

            }

            catch (Exception ex)

            {

                throw;

            }

        }

        public void BubbledException()

        {

            try

            {

                ActualException();

            }

            catch (Exception ex)

            {

                //throw new CommunicationFailureException("Cannot
access remote computer.",e)

                //throw ex;

                throw new CustomException("System is failure to process
the request"
, ex);

            }

        }

   
}

   
class Program

   
{

        static void Main(string[] args)

       {

            try

            {

                TestClass t = new TestClass();

                t.BubbledException();

                Console.ReadLine();
 
            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.Message + "\n");

                Console.WriteLine(ex.InnerException + "\n");

                Console.ReadLine();

            }
 
        }

   
}

}

Within yellow mark we are showing the custom message thrown by “StackOverflowException”. The message is pretty polite

“System is failure to process the request”.

But the important task is to log the actual exception for developer’s own purpose. We can do it by accessing InnerException property of Exception Object.

Within green box we are representing the actual Exception.

Exception

Conclusion

In this article we have learned to wrap actual exception thrown by code to represent polite custom exception. Hope this small trick will be helpful in your project implementation.

Next Recommended Readings