title: Using AssertionError and assertions in java
body: <p>I use assertions in Java in a standard way, having them turned on in my IDE. So they are not part of production release. Lately I have been seeing code examples with <code>throw new AssertionError()</code> and I started thinking about the situation where <code>AssertionError</code> should be used instead of assertion.</p>
  
  <p>My guess is that main difference is the optionality of asserts so they don't slow down the production performance and so they can occur quite often in the code, but fixing  hardly reproducible bugs reported from users is harder.</p>
  
  <p>For <code>AssertionError</code>, the exact opposite applies.</p>
  
  <p>I also find <code>AssertionError</code> more practical in places in code where the execution should not get, instead of using <code>assert false //We should not be here</code>. Especially if the return value is required. For example:</p>
  
  <pre><code>int getFoo(AnEnum a){
      if (a == AnEnum.ONE)
         return bar();
      else if (a == AnEnum.TWO)
         return SOME_VALUE;
      //else
      assert false; //throw new AssertionError();
      return -1; //not necessary when usin AssertionError
  }
  </code></pre>
  
  <ul>
  <li>Is my reasoning correct?</li>
  <li>What are the other differences/use cases/best practices/limitations
  of either approach?</li>
  <li>In regards to providing a description in the <code>AssertionError</code> - Should it be provided or is the mere fact that it is an <code>Error</code> (and
  of assertion type) enough to be more or less sure that stack trace
  will be provided in case of found bugs?</li>
  </ul>