Handle an Exception using try-catch Implementation with generic catch block




//Program to handle an Exception using try-catch Implementation with generic catch block
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAExceptions
{
    class ClsException2
    {
        static void Main()
        {
            int a, b, c;
            Console.WriteLine("Enter Any Two Numbers:-");
            try
            {
                a = Convert.ToInt32(Console.ReadLine());
                b = Convert.ToInt32(Console.ReadLine());
                c = a / b;
                Console.WriteLine("Quotient value is:-" + c);
            }
            catch
            {
                Console.WriteLine("Error Occured");
            }
            finally
            {
                Console.WriteLine("Code Executed");
            }
            Console.ReadLine();
        }
    }
}


Result:- 

 

  • In the above example there is no exception class used in catch block, so it is known as generic catch block
  • But in the above example any kind of exception may occur same message will be displayed to the user cannot understand why error has occurred, to overcome this specific catch blocks are used
  • Using specific catch blocks it is possible to know more information about the exceptions

Some important Exception Classes

  1. ArgumentException
  2. ArgumentNullException
  3. ArgumentOutOfRangeException
  4. ArithemeticException
  5. ArrayTypeMismatchException
  6. BadImageFormatException
  7. DivideByZeroException
  8. DllNotFoundException
  9. EntryPointNotFoundException
  10. Exception( Super class of all other exception classes)
  11. FormatException
  12. IndexOutOfRangeException
  13. InsufficientMemoryException
  14. InvalidCastException
  15. MemberAccessException
  16. NullReferenceException
  17. UnauthorizedAccessException
 Properties with Exception Classes

Message: This property will store about the reason, why exception has occurred

Source: This stores name of the application from which exception has been raised

HelpLink: This is used to provide link to any file/URL to give help information to the user, when exception is raised

No comments:

Post a Comment