HomeCSharpC# Error CS0157 – Control cannot leave the body of a finally clause

C# Error CS0157 – Control cannot leave the body of a finally clause

C# Compiler Error

CS0157 – Control cannot leave the body of a finally clause

Reason for the Error

You will receive this error when you have used the return statement inside the finally block.

The below code snippet demonstrates the C# error code CS0157 as it uses the return keyword with-in the finally block.

namespace ConsoleApp2
{
    class Program
    {
        public static void Main()
        {
            try
            {
 
            }
            finally
            {
                return;
            }          

        }

    }
    
}
C# Error CS0157 – Control cannot leave the body of a finally clause

Error CS0157 Control cannot leave the body of a finally clause ConsoleApp2 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp2\Program.cs 19 Active

Solution

C# specifies that all the statements inside the finally clause must execute. Having the return statement will break this rule. You will need to remove the return statement inside the finally block to fix this error.

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...