Tuesday, 3 March 2020

Difference between throw and throw ex in c# with simple example



Using throw statement-


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

namespace throw_throwex
{
    class Program
    {
        static void Main(string[] args)
        {
            //difference between throw and throw ex in c#
            try
            {
                int x = calc(5, 0);
                Console.WriteLine(x);
                Console.ReadLine();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static int calc(int a, int b)
        {
            return a / b;
        }
    }
}
Output:


Using throw ex statement-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace throw_throwex
{
    class Program
    {
        static void Main(string[] args)
        {
            //difference between throw and throw ex in c#
            try
            {
                int x = calc(5, 0);
                Console.WriteLine(x);
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static int calc(int a, int b)
        {
            return a / b;
        }
    }
}

About the Author

Admin

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment

 

Technical Talks © 2015 - Designed by Templateism.com, Distributed By Blogger Templates