Exception Handling:
Exception handling is the process of responding to the any Exception during computation, or exceptional conditions requiring special processing – often changing the normal flow of program execution.
PROGRAM:(ExceptionEgSingleCatch.cs)
using System;
namespace training
{
class ExceptionEgSingleCatch
{
public static void Main(string []args)
{
try
{
int[] arr=new int[2];//Fixed the size of Array
arr[3]=4; //Storing data out of the index but it is throw exception
}
catch(Exception e) //catch block execute when try block throws the error
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("Rest of the code");//Finally block execute after the execution of try or catch block
}
}
}
}
OUTPUT:
D:\csharp>ExceptionEgSingleCatch
Index was outside the bounds of the array.
Rest of the code
PROGRAM EXPLANATION:
The above program explain the exception handling and it take the index was outside the bounds of array,without exception handling concept it throw the runtime error but now it shows only the one line for that error,And before using the Exception handling the output was like following way.
PROGRAM WITHOUT USING EXCEPTION HANDLING CONCEPT:
using System;
namespace training
{
class ExceptionEgSingleCatch
{
public static void Main(string []args)
{
int[] arr=new int[2];//Fixed the size of Array
arr[3]=4;
}
}
}
OUTPUT:
D:\csharp>ExceptionEgSingleCatch
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at training.ExceptionEgSingleCatch.Main(String[] args)
Why Exception Handling?
We see above two examples ,the above examples are doing one operation (inserting data to the array),in that case poorly usage of exception handling but that the real time applications contains more number of operations that time this concept is very useful and that time really understand the features of Exception Handling.
Exception handling is the process of responding to the any Exception during computation, or exceptional conditions requiring special processing – often changing the normal flow of program execution.
PROGRAM:(ExceptionEgSingleCatch.cs)
using System;
namespace training
{
class ExceptionEgSingleCatch
{
public static void Main(string []args)
{
try
{
int[] arr=new int[2];//Fixed the size of Array
arr[3]=4; //Storing data out of the index but it is throw exception
}
catch(Exception e) //catch block execute when try block throws the error
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("Rest of the code");//Finally block execute after the execution of try or catch block
}
}
}
}
OUTPUT:
D:\csharp>ExceptionEgSingleCatch
Index was outside the bounds of the array.
Rest of the code
PROGRAM EXPLANATION:
The above program explain the exception handling and it take the index was outside the bounds of array,without exception handling concept it throw the runtime error but now it shows only the one line for that error,And before using the Exception handling the output was like following way.
PROGRAM WITHOUT USING EXCEPTION HANDLING CONCEPT:
using System;
namespace training
{
class ExceptionEgSingleCatch
{
public static void Main(string []args)
{
int[] arr=new int[2];//Fixed the size of Array
arr[3]=4;
}
}
}
OUTPUT:
D:\csharp>ExceptionEgSingleCatch
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at training.ExceptionEgSingleCatch.Main(String[] args)
Why Exception Handling?
We see above two examples ,the above examples are doing one operation (inserting data to the array),in that case poorly usage of exception handling but that the real time applications contains more number of operations that time this concept is very useful and that time really understand the features of Exception Handling.
No comments:
Post a Comment