Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2096

"main" should not "throw" anything

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this throws clause.
    • Default Severity:
      Blocker
    • Impact:
      High
    • Likelihood:
      High
    • Targeted languages:
      C#, C++, Objective-C, VB.Net
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      15min
    • Analysis Scope:
      Main Sources

      Description

      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[]) throws Exception { // Noncompliant
        doSomething();
      }
      

      Compliant Solution

      public static void main(String args[]) {
       try {
          doSomething();
        } catch (Throwable t) {
          log.error(t);
          System.exit(1);  // Default exit code, 0, indicates success. Non-zero value means failure.
        }
      }
      

        Attachments

          Issue Links

          1.
          C# RSPEC-5474 Language-Specification Closed Unassigned

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: