Module 7 - Exception Handling
Module 7 - Exception Handling
Exception Handling
By
SRIRAM . B
Overview
Exceptions
Exception Handling Keywords
Exception Types (System Defined, User Defined)
Using try and catch
Using try and finally
Handling try-catch-finally block
Throwing Exceptions
Exceptions
An exception is an erroneous situation that occurs during
program execution.
A division by zero
}
Using try and catch Block
try
{
//statements that may cause an exception
}
catch (…)
{
//error handling code
}
Using try and finally block
The finally block is used to execute a given set of
statements, irrespective of whether an exception is
thrown or not.
try
{
//statements that may cause an exception
}
finally
{
//statements to be executed
}
Handling try-catch-finally block
Result in Exception
Example 2 – Exception Handling
Using System;
namespace Exception Handling
{
public class excep1
{
static void Main(string[] arg)
{
int[] a ={ 2, 4, 12, 56, 23 };
for (int i = 0; i <= 5; i++)
try
{
Console.WriteLine("The {0}th element of the array
is {1}", i, a[i]);
}
catch (IndexOutOfRangeException e)
Example 2 – Exception Handling..
{
Console.WriteLine("An Exception has occured");