C# | How to use multiple catch clause
The main purpose of the catch block is to handle the exception raised in the try block. This block is only going to execute when the exception raised in the program.
In C#, You can use more than one catch block with the try block. Generally, multiple catch block is used to handle different types of exceptions means each catch block is used to handle different type of exception. If you use multiple catch blocks for the same type of exception, then it will give you a compile-time error because C# does not allow you to use multiple catch block for the same type of exception. A catch block is always preceded by the try block.
In general, the catch block is checked within the order in which they have occurred in the program. If the given type of exception is matched with the first catch block, then first catch block executes and the remaining of the catch blocks are ignored. And if the starting catch block is not suitable for the exception type, then compiler search for the next catch block.
Syntax:
try {
// Your code
}
// 1st catch block
catch(Exception_Name) {
// Code
}
// 2nd catch block
catch(Exception_Name) {
// Code
}
.
.
.
.
Below given are some examples to understand the implementation in a better way:
Example 1: In the below example, try block generate two different types of exception i.e DivideByZeroException and IndexOutOfRangeException. Now we use two catch blocks to handle these exceptions that are associated with a single try block. Each catch block caught a different type of exception like catch block 1 is used to catch DivideByZeroException, catch block 2 is used to catch IndexOutOfRangeException.
using System;
class GFG {
static void Main()
{
int [] number = { 8, 17, 24, 5, 25 };
int [] divisor = { 2, 0, 0, 5 };
for ( int j = 0; j < number.Length; j++)
try {
Console.WriteLine( "Number: " + number[j]);
Console.WriteLine( "Divisor: " + divisor[j]);
Console.WriteLine( "Quotient: " + number[j] / divisor[j]);
}
catch (DivideByZeroException) {
Console.WriteLine( "Not possible to Divide by zero" );
}
catch (IndexOutOfRangeException) {
Console.WriteLine( "Index is Out of Range" );
}
}
}
|
Output:
Number: 8
Divisor: 2
Quotient: 4
Number: 17
Divisor: 0
Not possible to Divide by zero
Number: 24
Divisor: 0
Not possible to Divide by zero
Number: 5
Divisor: 5
Quotient: 1
Number: 25
Index is Out of Range
Example 2: In the below example, try block raise an exception. So we will use three different type of catch blocks to handle the exception raised by the try block. Catch block 1 will handle IndexOutOfRangeException, catch block 2 will handle FormatException, and catch block 3 will handle OverflowException.
using System;
class GFG {
static void Main()
{
try {
byte data = byte .Parse( "a" );
Console.WriteLine(data);
}
catch (IndexOutOfRangeException) {
Console.WriteLine( "At least provide one Argument!" );
}
catch (FormatException) {
Console.WriteLine( "Entered value in not a number!" );
}
catch (OverflowException) {
Console.WriteLine( "Data is out of Range!" );
}
}
}
|
Output:
Entered value in not a number!
Last Updated :
24 Jan, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...