Showing posts with label Exception. Show all posts
Showing posts with label Exception. Show all posts

Tuesday, 10 March 2020



The code snippet is given as below.


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

namespace CustomExceptionInCsharp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int res = checkAGE();
                Console.WriteLine(res);
                Console.ReadLine();
            }
            catch (CustomEx_AGE ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        static int checkAGE()
        {
            Console.WriteLine("Enter AGE:");
            int age = Convert.ToInt32(Console.ReadLine());
            if (age<18 18="" :="" above="" age="" base="" be="" class="" customex_age="" exception="" msg="" new="" pre="" public="" return="" should="" string="" throw="">

How to create Custom Exception in C# ?

The code snippet is given as below. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System...

Tuesday, 3 March 2020



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;
        }
    }
}

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.T...

 

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