How to use multiple catch clauses in C#

In this article, we will discuss how to use multiple catch blocks in a C# program with some examples. But before discussing the implementation of multiple catch blocks, we must know about the catch block.

What is a Catch Block?

Every catch block serves as an exception handler that deals with the specific type of Exception specified by its arguments. An exception handler can be assigned a particular type of exception by passing the Type argument. It must be the name of the class that inherits from the Throwable class. Handlers can refer to exceptions by name. A catch block is a segment of code that executes upon the exception handler being called. The runtime system calls an exception handler if the handler is the first exception handler in the call stack whose ExceptionType matches the type of exception that was thrown. In this case, the system considers the thrower object a match if it can be legally associated with an exception handler argument.

In C#, we can use multiple Catch blocks using Try blocks. Multiple catch blocks are commonly utilized to handle several types of exceptions. Using multiple catch blocks for the same exception type will result in a compile-time error because C# does not enable multiple catch blocks for the same exception type. A try block always follows a catch block. In general, catch blocks are checked the order in which they appear in the program. If the specified exception type matches the first catch block, the first catch block is executed, and then the remaining catch blocks are ignored. When the initial catch block does not match the exception type, the compiler seeks the next catch block.

Syntax:

It has the following syntax:

Examples:

Below are some examples to better understand the implementation of catch blocks in C#.

Example 1: The following example attempts to generate two different types of exceptions: DivideByZeroException and IndexOutOfRangeException. We will use two Catch blocks associated with one Try block to handle these exceptions.

The catch block was subject to different types of exceptions. Catch block 1 is utilized to catch DivideByZeroException, and catch block 2 is utilized to handle the catch IndexOutOfRangeException.

Filename: Catch.cs

Output:

The Number: 18
The Divisor value: 9
The Quotient is: 2
The Number: 19
The Divisor value: 0
Division by zero is impossible.
The Number: 28
The Divisor value: 1
The Quotient is: 28
The Number: 35
The Divisor value: 0
Division by zero is impossible.
The Number: 56
The Index value is Out of Range

Explanation:

Array initialization: In this example, two arrays (numbers and divisors) are initialized with integer values.

For Loop: The program uses a for loop to loop over the elements of an array, a try block, and numbers[j] / divisors(J) (this instruction is taken from within the loop).

Catch Block: The Try block is followed by two catch blocks. The Catch block has a distinct set of exceptions that can be found while executing the code in the Try block.

The catch block 1 catches the DivideByZeroException and prints a message indicating that division by zero is not possible.

It is followed by the second catch block, which prints an error 'IndexOutOfRangeException' that throws the index value out of range.

Continuation of Loop: After that, the program can continue with the next loop even if an exception is found, enabling it to process each element in the array.

Example 2:

The following example describes the try block exception. Therefore, we use three different types of catch blocks to handle exceptions thrown by try blocks. Catch block 1 handles IndexOutOfRangeException, catch block 2 handles FormatException, and catch block 3 handles OverflowException.

Filename: MutliCatchBlock.cs

Output:

The value entered is not a number!

Explanation:

  • In this example, the Try block attempts to convert the string "a" into (byte.Parse("a");) by using val = byte. This line of code experiences a FormatException due to the absence of corresponding data representation for the "a" bytes.
  • Catch Block: A Catch block follows a Try block and is used to handle certain types of exceptions that a Try block can throw.
  • Catch block 1 (IndexOutOfRangeException): It handles IndexOutOfRangeException by printing the "important argument value".
  • Exception for Block 2 (FormatException): The value entered is not a numerical quantity.
  • Catch Block 3 (OverflowException): It handles the OverflowException by outputting "Data is out of range".
  • If a try block contains an exception, the program jumps to its catch block, which is determined by the type of exception thrown. In this case, the exception is of type FormatException, so control passes to catch block 2, which displays "The value entered is not a number".

Conclusion:

In conclusion, the multi-catch block handles different types of exceptions. Each catch block handles several types of exceptions, allowing us to create multiple catch blocks.






Latest Courses