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

Throwable and Error should not be caught

    XMLWordPrintable

    Details

    • Message:
      Catch Exception instead of [Throwable|Error]
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      C, C++, Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      20min
    • Analysis Scope:
      Main Sources
    • CERT:
      ERR08-J.
    • CWE:
      CWE-396
    • CppCoreGuidelines:
      E.14
    • PMD:
      AvoidCatchingThrowable

      Description

      Throwable is the superclass of all errors and exceptions in Java. Error is the superclass of all errors, which are not meant to be caught by applications.

      Catching either Throwable or Error will also catch OutOfMemoryError and InternalError, from which an application should not attempt to recover.

      Noncompliant Code Example

      try { /* ... */ } catch (Throwable t) { /* ... */ }  
      try { /* ... */ } catch (Error e) { /* ... */ }   
      

      Compliant Solution

      try { /* ... */ } catch (RuntimeException e) { /* ... */ }
      try { /* ... */ } catch (MyException e) { /* ... */ }
      

      See

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              freddy.mallet Freddy Mallet (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated: