Advertisement
Advertisement
//C# Nested try-catch block
using System;
class MultipleCatch2
{
public static void Main(String[] stArr)
{
//Outer try block
try
{
int a = 100, b = 10;
int total = a + b;
//A nested try block
try
{
//Defining an array
int[] arr = {1,2,3,4};
//Trying to access an invalid index, 5, so, the IndexOutOfRangeException will be thrown
Console.WriteLine("Value ="+arr[5]);
}
//A nested catch block to handle a specific subclass of Exception class, "IndexOfRangeException"
catch(IndexOutOfRangeException exp)
{
Console.WriteLine("Exception Caught - "+ exp);
}
}
//An outer catch block to handle all exceptions, with superclass "Exception"
catch(IndexOutOfRangeException exp)
{
Console.WriteLine(exp);
}
}
}
Exception Caught - System.IndexOutOfRangeException: Index was outside the bounds of the array.
at MultipleCatch2.Main(String[] stArr)
//C# A nested
using System;
class MultipleCatch2
{
public static void Main(String[] stArr)
{
//Outer try block
try
{
int a = 100, b = 10;
int total = a + b;
//A nested try block
try
{
//Defining an array
int[] arr = {1,2,3,4};
//Trying to access an invalid index, 5, so, the IndexOutOfRangeException will be thrown
Console.WriteLine("Value ="+arr[5]);
}
//A nested catch block to handle a specific subclass of Exception class, "ArithmeticException"
catch(ArithmeticException exp)
{
Console.WriteLine("Exception caught in the nested catch block: - ");
Console.WriteLine(exp);
}
}
//An outer catch block to handle a specific subclass of Exception class, "IndexOutOfRangeException"
catch(IndexOutOfRangeException exp)
{
Console.WriteLine("Exception caught in the outer catch block: ");
Console.WriteLine(exp);
}
}
}
Exception caught in the outer catch block:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at MultipleCatch2.Main(String[] stArr)
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement