Exception handling technique between class library and other projects

The following two examples generate same StackTrace

//Example - 1 ==========================================
namespace MyLibrary
{
   public class TestClass
    {
       private void DoItNow()
       {
           throw new InvalidOperationException("I am an exception.");
       }

       public void DoSomething()
       {
           DoItNow();
       }
    }
}

//Example - 2 ==========================================
namespace MyLibrary
{
   public class TestClass
    {
       private void DoItNow()
       {
           try
           {
               throw new InvalidOperationException("I am an exception.");
           }
           catch (Exception)
           {
               throw;
           }
       }

       public void DoSomething()
       {
           try
           {
               DoItNow();
           }
           catch (Exception)
           {
               throw;
           }
       }
    }
}

//User project.
namespace DeleteNow
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                MyLibrary.TestClass testClass = new MyLibrary.TestClass();
                testClass.DoSomething();
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.Message);
                Console.WriteLine(Ex.StackTrace);
            }

            Console.ReadLine();

        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s