In this article I will explain with an example, how to handle the exceptions using Switch Case statement in ASP.Net Core (.Net Core 7) MVC.
Note: For beginners in ASP.Net Core (.Net Core 7), please refer my article ASP.Net Core 7: Hello World Tutorial with Sample Program example.
 
 

What is Switch Case statement in .Net?

The switch is a keyword and by using it, one can create conditional statements with multiple blocks and the multiple blocks can be constructed by using the case keyword.
It evaluates a given expression and based on the evaluated value (matching a certain condition), it executes the statements associated with it.
Basically, it is used to perform different actions based on different conditions (cases).
1. The Switch Case statement follows selection based mechanism and allows a value to change control of execution.
2. It is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
In .Net, it is used for executing one condition from multiple conditions. It is similar to an if-else-if conditions.
It consists of conditional-based cases and a default case.
 
 

Syntax of Switch Case statement

The Switch Case expression is of type such as int, byte, short, enumeration type, character type or string type.
The expression is checked for different cases and the matched case will be executed.
Following is the syntax to use Switch Case statement in .Net.
Exception Handling in .Net Core using Switch Case statement
 
 

Rules of Switch Case statement

Following are some of the rules that we need to follow while using the Switch Case statement:
1. In a Switch Case statement, the “case value” must be of “char”, “int” and “string” type.
2. There can be one or N number of cases.
3. The values in the case must be unique from each other.
4. Each statement of the case can have a break statement. It is optional.
5. The default statement is also optional.
 
 

Handling Exceptions using Switch Case statement

In the following example, an exception is raised by converting a string value to integer inside a Try-Catch block.
Note: For more details on handling exception in .Net Core, please refer my article Exception Handling in .Net Core.
 
The raised exception is caught in the Catch block where Switch Case statement is executed.
This type of exception is called System.FormatException.
The raised exception is caught in the 1st case.
public class HomeController : Controller
{
    private IWebHostEnvironment Environment;
    public HomeController(IWebHostEnvironment _environment)
    {
        this.Environment = _environment;
    }
    public IActionResult Index()
    {
        try
        {
            // Code that might throw Error.
            int i = int.Parse("Mudassar");
        }
        catch (Exception ex)
        {
            switch (ex.GetType().ToString())
            {
                case "System.FormatException":
                    // Handle format Exception.
                    break;
                case "System.ArgumentNullException":
                    // Handle null argument Exception.
                    break;
                case "System.IO.FileNotFoundException":
                    // Handle file not found Exception.
                    break;
                default:
                    break;
            }
        }
        finally
        {
        }
        return View();
    }
}
 
 

Screenshot

Exception Handling in .Net Core using Switch Case statement
 
 

Downloads