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

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...