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

"RuntimeException" should not be caught

    XMLWordPrintable

    Details

    • Type: Finding Detection
    • Status: Closed
    • Resolution: Won't Fix
    • Message:
      Remove this "catch" statement
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Targeted languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      15 min
    • CWE:
      396

      Description

      {{RuntimeException}}s are generally caused by programmer error or problems with the underlying system running the program, such as running out of disk space for the logs. As such, they are not expected to be thrown in the normal course of program execution, and there is generally very little that can be done to recover from them. Depending on the cause of the exception, even an attempt to log it may fail. Therefore the best course is to code to avoid them as much as possible.

      If there are specific unchecked exceptions that your program may actually be able to recover from, then they should be caught and handled specifically, rather than catching the parent class, RuntimeException.

      Noncompliant Code Example

      try {
        EventQueue.invokeAndWait(new Runnable() {
          public void run() {
              createAndShowGUI();
          }
        });
      } catch (RuntimeException ex ) {  // Noncompliant
        // 
      }
      

      Compliant Solution

      try {
        EventQueue.invokeAndWait(new Runnable() {
          public void run() {
              createAndShowGUI();
          }
        });
      } catch (DatabindingException ex ) {  // Noncompliant
        // 
      }
      

      See

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: