How to Catch an Exception in C# ?


This content was proof-read by Grammarly. You can Try Grammary for FREE

Every time when the exception is thrown , it is necessary that it be handled . In this case , one needs to wrap the code with the try catch block as shown below.

How to Catch an Exception in C# ?

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

namespace AbundantcodeConsole
{ 
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int input = 10;
                int val = 0;
                int Output = input / val;
            }
            catch(DivideByZeroException ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }

        }

    }  
}
1
%d bloggers like this: