39 lines
1.0 KiB
Plaintext
39 lines
1.0 KiB
Plaintext
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
|
|
|
|
----
|
|
public static void Main(string[] args) { // Noncompliant
|
|
if (args.Length == 0)
|
|
{
|
|
throw new ArgumentException();
|
|
}
|
|
// do stuff
|
|
}
|
|
----
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
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[]
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|