Exception Handling

An exception is a problem that arises during the execution of a program. Exception handling provides a way to transfer control from one part of a program to another when it comes across an exception.

An exception is a problem that arises during the execution of a program. Exception handling provides a way to transfer control from one part of a program to another when it comes across an exception.

Exception handling is implemented by using try catch logic.

  1. Try: Checks for an exception.
  2. Catch: Catches an exception with an exception handler
  3. Finally: executes a given set of statements irrespective of exception or not.
  4. Throw: A program that throws an exception when executed.

Types of exceptions classes:

Exception ClassException Description
System.IO.IOExceptionHandles I/O errors.
System.IndexOutOfRangeExceptionHandles errors generated when a method refers to an array index out of range.
System.ArrayTypeMismatchExceptionHandles errors generated when the type is mismatched with the array type.
System.NullReferenceExceptionHandles errors generated from referencing a null object.
System.DivideByZeroExceptionHandles errors generated from dividing a dividend with zero.
System.InvalidCastExceptionHandles errors generated during typecasting.
System.OutOfMemoryExceptionHandles errors generated from insufficient free memory.
System.StackOverflowExceptionHandles errors generated from stack overflow.
using System;

namespace ErrorHandling
{
    public class divideNumber
    {
        public int result { get; set; }

        public divideNumber()
        {
            result = 0;
        }

        public void division (int num1, int num2)
        {
            try
            {
                result = num1 / num2;
            }
            catch(Exception e)
            {
                Console.WriteLine("Exception caught: {0}", e);
            }
            finally
            {
                Console.WriteLine("Exception has been logged successfully");
            }
        }

        static void Main(string[] args)
        {
            divideNumber d = new divideNumber();
            d.division(14, 0);
            Console.ReadKey();
        }
    }
}

User defined Exception:

using System;

namespace ErrorHandling
{
    public class Students
    {
        public int StudentId { get; set; }
        public string StudentName { get; set; }
    }

    class InvalidNameException : Exception
    {
        public InvalidNameException()
        {

        }

        public InvalidNameException(string name) : base (string.Format("Invalid Student Name: {0}", name))
        {

        }
    }
    
    class Program
    { 
        private static void Validate(Students stu)
        {
            if(stu.StudentName == "Tejaswi123")
            {
                throw new InvalidNameException(stu.StudentName);
            }
        }
        static void Main(string[] args)
        {
            Students newStudent = new Students();

            try
            {
                newStudent.StudentName = "Tejaswi123";

                Validate(newStudent);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }
    }
}

One thought on “Exception Handling

Leave a Reply

Your email address will not be published. Required fields are marked *