Tuesday, 5 August 2014

Examples on Types of Exception Handling

    Examples on Types of Exception Handling

1)Example of SystemIO.IOException:-

using System;
using System.IO;

class Program
{
    static void Main(String[] args)
    {
        try
        {
            File.Open("C:\\anuragdhamal\\new.txt", FileMode.Open);
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("Not found");
        }
        catch (IOException)
        {
            Console.WriteLine("IO");
        }

    }
}

output:
IO
Press Any Key To Countinue....

2) Example Of System.IndexOutofRange:-

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

namespace ConsoleApplication5_IndexOutOfRangeExc
{

    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = new int[2];
            try
            {
                numbers[0] = 11;
                numbers[1] = 21;
                //  numbers[2] = 24; // here if we uncomment this line then exception is gets raised

                foreach (int i in numbers)
                    Console.WriteLine(i);
            }
            catch (IndexOutOfRangeException ex)
            {
                Console.WriteLine("An index out of range! occure" + ex);
                Console.WriteLine("HelpLink = {0}", ex.HelpLink);
                Console.WriteLine("Message = {0}", ex.Message);
                Console.WriteLine("Source = {0}", ex.Source);
                Console.WriteLine("StackTrace = {0}", ex.StackTrace);
            }

            finally
            {
                Console.WriteLine("Task completed");
            }
            Console.ReadLine();

        }
    }
}

Output:-
11
21
Task Complete

3)Example Of SystemArrayTypeMismatchException:-

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

namespace ConsoleApplication5_ArrayTypeMismatchExc
{
    class Class1
    {
        static void Main(string[] args)
        {
            string[] names = { "Anurag", "Dhamal", "compulink" };
            object[] objs = (object[])names;
            try
            {
                objs[2] = "Academy";
                foreach (object Name in objs)
                {
                    Console.WriteLine(Name);
                }
            }
            catch (ArrayTypeMismatchException e1)
            {
                // Not reached; "Academy" is of the correct type.
                System.Console.WriteLine("Exception Thrown.");
            }
            try
            {
                Object obj = (Object)13;
                objs[2] = obj;
            }
            catch (ArrayTypeMismatchException e2)
            {
                Console.WriteLine(e2.Message);
                // Always reached, 13 is not a string.
                Console.WriteLine("New element is not of the correct type.");
            }
            // Set objs to an array of objects instead of
            // an array of strings.
            objs = new Object[3];
            try
            {
                objs[0] = (Object)"Shirwal";
                objs[1] = (Object)8;
                objs[2] = (Object)12.12;
                foreach (object element in objs)
                {
                    Console.WriteLine(element);
                }
            }
            catch (ArrayTypeMismatchException)
            {
                // ArrayTypeMismatchException is not thrown this time.
                System.Console.WriteLine("Exception Thrown.");
            }
            Console.ReadLine();
        }
    }
}
output:-









No comments:

Post a Comment