Wednesday 21 December 2016

throw and throw ex in Csharp Example

By exploring given example we could say that

  • throw; preserve its pointer, i mean StackTrace from oregional source.
  • Where as with throw warper we loose StackTrace.



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

namespace throw_and_throw_ex_in_Csharp
{
    class Program
    {
        public static void devicebyzero()
        {
            int devideby = 0;
            int a = 9 / devideby; //line no 14
            Console.WriteLine(a);
        }
        static void Main(string[] args)
        {
            try
            {
                devicebyzero(); // line no 21
            }
            catch (Exception)
            {
                throw; //line no 25
            }
            //StackTrace 
            //at throw_and_throw_ex_in_Csharp.Program.devicebyzero() in E:\C# Examples\throw and throw ex in Csharp\throw and throw ex in Csharp\Program.cs:line 14
            //at throw_and_throw_ex_in_Csharp.Program.Main(String[] args) in E:\C# Examples\throw and throw ex in Csharp\throw and throw ex in Csharp\Program.cs:line 25

            //try
            //{
            //    devicebyzero();//line no 33 
            //}
            //catch (Exception ex)
            //{
            //    throw ex;// line no 37
            //}
            //at throw_and_throw_ex_in_Csharp.Program.Main(String[] args) in E:\C# Examples\throw and throw ex in Csharp\throw and throw ex in Csharp\Program.cs:line 37
            //at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
            //at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)


            //try
            //{
            //    devicebyzero(); // line no 46
            //}
            //catch (Exception ex)
            //{
            //    throw new Exception("Error in 3rd block");//line no 50
            //}
            //at throw_and_throw_ex_in_Csharp.Program.Main(String[] args) in E:\C# Examples\throw and throw ex in Csharp\throw and throw ex in Csharp\Program.cs:line 50
            //at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
            //at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)



            //try
            //{
            //    devicebyzero();//line no 60
            //}
            //catch (Exception ex)
            //{
            //    throw new Exception("Error in 3rd block",ex);//line no 64
            //}
            //at throw_and_throw_ex_in_Csharp.Program.Main(String[] args) in E:\C# Examples\throw and throw ex in Csharp\throw and throw ex in Csharp\Program.cs:line 64
            //at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
            //at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)


            //Console.Read();



        }
    }
}