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

IllegalMonitorStateException should not be caught

    XMLWordPrintable

    Details

    • Message:
      Refactor this code to not catch IllegalMonitorStateException.
    • Default Severity:
      Critical
    • Impact:
      High
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      20min
    • Analysis Scope:
      Main Sources
    • FindBugs:
      IMSE_DONT_CATCH_IMSE

      Description

      According to Oracle Javadoc:

      IllegalMonitorStateException is thrown when a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.

      In other words, this exception can be thrown only in case of bad design because Object.wait(...), Object.notify() and Object.notifyAll() methods should never be called on an object whose monitor is not held.

      Noncompliant Code Example

      public void doSomething(){
        ...
        try {
          ...
          anObject.notify();
          ...
        } catch(IllegalMonitorStateException e) {
          ...
        }
      }
      

      Compliant Solution

      public void doSomething(){
        ...
        synchronized(anObject) {
          ...
          anObject.notify();
          ...
        }
      }
      

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated: