0% found this document useful (0 votes)
3 views3 pages

Exception Handling

The document explains exception handling in programming, defining it as a method to manage errors that occur during program execution. It outlines key concepts such as try, catch, finally, and throw, and provides code examples demonstrating the use of multiple try-catch blocks to handle various exceptions. The importance of exception handling is emphasized as it allows programs to continue functioning gracefully despite unexpected events.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Exception Handling

The document explains exception handling in programming, defining it as a method to manage errors that occur during program execution. It outlines key concepts such as try, catch, finally, and throw, and provides code examples demonstrating the use of multiple try-catch blocks to handle various exceptions. The importance of exception handling is emphasized as it allows programs to continue functioning gracefully despite unexpected events.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Exception Handling

An exception is defined as an error or unexpected event that occurs during the execution of a program. The actions
to be performed in case of the occurrence of an exception is not known to the program. In such a case, we create
an exception object and call the exception handler code. The execution of an exception handler so that the program
code does not crash is called exception handling. Exception handling is important because it gracefully handles
an unwanted event, an exception so that the program code still makes sense to the user.

Keyword Definition

try Used to define a try block. This block holds the code that may throw an exception.

catch Used to define a catch block. This block catches the exception thrown by the try block.

finally Used to define the finally block. This block holds the default code that will run in both cases
whether exception occurs or not

throw Used to create and throw an exception manually.

The code example given below shows how we can handle exceptions using the try-catch block. The code that
may generate an exception is placed inside the try block. In this case, we are accessing the 7th element of the
array inside the try block which does not exists. When that statement is executed an exception is generated, which
is caught by the catch block. The type of exception object is IndexOutOfRangeException which is displayed as a
message to the user about the exception that has occurred.
using System;
class Program : System.Exception {
static void Main(string[] args)
{ int[] arr = { 1, 2, 3, 4, 5 }; // Declare an array of 4 values
try {
// Display values of array elements
for (int i = 0; i < arr.Length; i++)
{ Console.WriteLine(arr[i]); }
// Try to access invalid index of array
arr[7] = 100; // An exception is thrown upon executing the above line
}
catch (Exception ex) {

// The ex.Message property is used to display the type of exception


// that has occurred to the user.
MessageBox.Show("An Exception has occurred :" + ex.Message);
}
}
}
Using Multiple try-catch blocks

In the code given below, we attempt to generate an exception in the try block and catch it in one of the multiple
catch blocks. Multiple catch blocks are used when we are not sure about the exception type that may be generated,
so we write different blocks to tackle any type of exception that is encountered. The finally block is the part of
the code that has to be executed irrespective of if the exception was generated or not.
// C# Program to show use of multiple try catch blocks
using System;
class Program {
static void Main(string[] args)
{
int[] arr = {19, 0, 75, 52};
try {
// Try to generate an exception
for (int i = 0; i < arr.Length; i++) {
MessageBox.Show(arr[i] / arr[i + 1]);
}
}
// Catch block for invalid array index access
catch (IndexOutOfRangeException iore)
{
MessageBox.Show("An Exception has occurred : " + iore.Message);
}
// Catch block for attempt to divide by zero
catch (DivideByZeroException dbz) {
MessageBox.Show("An Exception has occurred : " + dbz.Message);
}
// Catch block for value being out of range
catch (ArgumentOutOfRangeException aor)
{
MessageBox.Show("An Exception has occurred : " + aor.Message);
}
Catch (Exception ex) // Catch block for every type of exception comes at last if
{ //there are multiple catch blocks
MessageBox.Show("An Exception has occurred : " + ex.Message);
}
// Finally block will run in both cases whether error occurs or not
finally {
for (int i = 0; i < arr.Length; i++) {
MessageBox.Show(arr[i].ToString());
}
The code above demonstrates the use of multiple try-catch blocks along with a finally block in C#. For an integer
array called arr with four elements. Inside the try block, the code attempts to divide each element of the array by
the next element (arr[i] / arr[i + 1]) in a loop. This operation is likely to cause exceptions, such as dividing by
zero or accessing an array index that is out of bounds. To handle these potential errors, several catch blocks are
provided. Each catch block is designed to handle a specific type of exception: IndexOutOfRangeException for
invalid array access, DivideByZeroException for division by zero, and ArgumentOutOfRangeException for
values that are out of the expected range. There is also a general Exception catch block to handle any other
exceptions that might occur. Each catch block prints a message to the console describing the exception. Finally,
the finally block contains a loop that prints all the elements of the array to the console. The code in the finally
block will always execute, regardless of whether an exception was thrown or not. This ensures that the contents
of the array are displayed to the user under all circumstances.

You might also like