rspec/rules/S2096/csharp/rule.adoc

50 lines
1.2 KiB
Plaintext

== 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)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]