== Why is this an issue?

There's no reason for a ``++Main++`` method to ``++throw++`` anything. After all, what's going to catch it? 


Instead, the method should itself gracefully handle any exceptions that may bubble up to it, attach as much contextual information as possible, and perform whatever logging or user communication is necessary, and ``++Exit++`` with a non-zero (i.e. non-success) exit code if necessary.

=== Noncompliant code example

[source,csharp]
----
public static void Main(string[] args) { // Noncompliant
  if (args.Length == 0)
  {
    throw new ArgumentException();
  }
  // do stuff
}
----

=== Compliant solution

[source,csharp]
----
private const int ERROR_INVALID_COMMAND_LINE = 0x667;
public static void Main(string[] args) { // Noncompliant
  if (args.Length == 0)
  {
    Console.WriteLine("Should provide at least one argument");
    Environment.ExitCode = ERROR_INVALID_COMMAND_LINE;  
  }
  // do stuff
}
----

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::../message.adoc[]

'''
== Comments And Links
(visible only on this page)

=== on 30 Sep 2019, 09:20:07 Andrei Epure wrote:
\[~nicolas.harraudeau] - this rule is not as useful for Java, where there's the ``++throws++`` keyword in the method signature if there is a call to a method that throws an exception. In C#, there's no such mechanism, so we can just look if there's a ``++throw++`` inside the method body. Is this useful enough?

=== on 30 Sep 2019, 10:45:16 Nicolas Harraudeau wrote:
\[~andrei.epure] indeed the value of this rule is lower. The only real-world use case I see is if there is a ``++throw++`` in a ``++try-catch++`` block and the exceptions caught do not match the one thrown. => Unintentional throw outside of the try-catch block. This is a corner case which will happen very rarely so there is no point in implementing the rule for C#.

endif::env-github,rspecator-view[]
