While creating any software, developers try to create application which doesn't stop by any application error, so they handle errors using Try/Catch block in C#, but usually in old C# code, we have to implement multiple catch for each exception, so in this article, I have mentioned how you can catch multiple exceptions in C# using single catch block.
Suppose we have some code and we have single try block with multiple catch exceptions, since the code inside try{} block can throw multiple exceptions, so it would look like below
try
{
// Some Code which can throw multiple exceptions
}
catch (FormatException ex)
{
// handle format exceptions
}
catch (OverflowException ex)
{
// handle overflow exception
}
catch (DivideByZeroException)
{
// handle divide by zero exception
}
But as you can see from above code structure, we have repeated code, so to avoid it, if you are using C# 6+, then you can simply use single catch block for multiple exceptions.
Take a look at an example code below
using System;
namespace MultipleCatchCsharp
{
public class Program
{
static void Main(string[] args)
{
try
{
var numerator = Convert.ToUInt32("12");
var denominator = Convert.ToUInt32("0");
var result = (int)(numerator / denominator);
Console.WriteLine(result);
}
catch (Exception ex) when (ex is FormatException ||
ex is DivideByZeroException ||
ex is OverflowException)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
output:
Error: Attempted to divide by zero.
In the above code, we are trying to divide by zero, after converting string into int in C# and exception "DivideByZeroException" occured.
let's try to update the code to get FormatException, it would look like below
try
{
var numerator = Convert.ToUInt32("12");
var denominator = Convert.ToUInt32("10d");
var result = (int)(numerator / denominator);
Console.WriteLine(result);
}
catch (Exception ex) when (ex is FormatException ||
ex is DivideByZeroException ||
ex is OverflowException)
{
Console.WriteLine("Error: " + ex.Message);
}
and the output will be
Error: Input string was not in a correct format.
Since we are trying to convert decimal into int.
So from the above code example, we can see a single catch block can be used to handle multiple exceptions in C#.
Updating Format to get separate exception messages for each Exception
We can also use Switch
case inside Catch
block, to separate exceptions code, as shown below
using System;
namespace MultipleCatchCsharp
{
public class Program
{
static void Main(string[] args)
{
try
{
var numerator = Convert.ToUInt32("12");
var denominator = Convert.ToUInt32("10d");
var result = (int)(numerator / denominator);
Console.WriteLine(result);
}
catch (Exception ex)
{
switch (ex)
{
case FormatException:
Console.WriteLine("Error: Format Exception");
break;
case DivideByZeroException:
Console.WriteLine("Error: Divide By Zero Exception");
break;
case OverflowException:
Console.WriteLine("Error: Overflow Exception");
break;
}
}
}
}
}
Output:
Error: Format Exception
You may also like to read: