Advertisement
//C# Multiple Catch Block Example
using System;
using System.IO;
class MultipleCatch
{
public static void Main(String[] stArr)
{
try
{
//Defining an array
int[] arr = {1,2,3,4};
//Trying to access an invalid index 5, an IndexOutOfRangeException will be thrown
Console.WriteLine("Value ="+arr[5]);
}
catch(ArrayTypeMismatchException exp) //catch block to handle/catch ArrayTypeMismatchException
{
Console.WriteLine("An ArrayTypeMismatchException is caught");
}
catch(IndexOutOfRangeException exp) //catch block to handle/catch IndexOutOfRangeException
{
Console.WriteLine("An IndexOutOfRangeException is caught");
}
catch(Exception exp) //A general catch block to handle/catch Exception
{
Console.WriteLine("An Exception is caught");
}
}
}
An IndexOutOfRangeException is caught
Advertisement
//C# When defining multiple catch blocks,
//the placement of each catch block is very important
using System;
class MultipleCatch2
{
public static void Main(String[] stArr)
{
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 general catch block to handle/catch all exceptions, with superclass "Exception"
catch(Exception exp)
{
Console.WriteLine("Exception Caught - "+ exp);
}
//A catch block to handle a specific subclass of Exception class, "IndexOfRangeException"
catch(IndexOutOfRangeException exp)
{
Console.WriteLine(exp);
}
}
}
MultipleCatch2.cs(25,8): error CS0160: A previous catch clause already catches all exceptions of this or of a super type ('Exception')
//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