HomeCSharpC# Error CS0156 – A throw statement with no arguments is not allowed outside of a catch clause

C# Error CS0156 – A throw statement with no arguments is not allowed outside of a catch clause

C# Compiler Error

CS0156 – A throw statement with no arguments is not allowed outside of a catch clause

Reason for the Error

You will receive this error in C# when you try to throw an exception outside of a catch block.

For example, try compiling the below code snippet.

using System;

namespace ConsoleApp2
{
    public class DeveloperPublishException
    {
      
    }
    class Program
    {
        public static void Main()
        {
            try
            {
                throw;  
            }

            catch (Exception ex)
            {
                
            }           

        }

    }
    
}

You will receive the C# error code CS0156 because the throw keyword was used outside the catch block (inside the try)

C# Error CS0156 – A throw statement with no arguments is not allowed outside of a catch clause

Error CS0156 A throw statement with no arguments is not allowed outside of a catch clause ConsoleApp2 C:\Users\Senthil\source\repos\ConsoleApp1\ConsoleApp2\Program.cs 15 Active

Solution

To fix the error, move the logic of where you are calling the throw without parameters inside the catch block.

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...