title: Appropriate use of assert
body: <p>Can you please help me better understand, what is an appropriate use of “assert” vs “throwing an exception? When is each scenario appropriate?</p>
  
  <p><strong>Scenario 1</strong></p>
  
  <p>CODE</p>
  
  <pre><code>public Context(Algorythm algo) {
    if (algo == null) {
        throw new IllegalArgumentException("Failed to initialize Context");
    }
    this.algo = algo;
  }
  </code></pre>
  
  <p>TEST</p>
  
  <pre><code>public void testContext_null() {
    try {
        context = new Context(null);
        fail();
    } catch (IllegalArgumentException e) {
        assertNotNull(e);
    }
  }
  </code></pre>
  
  <p><strong>Scenario 2</strong></p>
  
  <p>CODE</p>
  
  <pre><code>public Context(Algorythm algo) {
    assert (algo != null);
    this.algo = algo;
  }
  </code></pre>
  
  <p>TEST</p>
  
  <pre><code>public void testContext_null() {
    try {
        context = new Context(null);
        fail();
    } catch (AssertionFailedError e) {
        assertNotNull(e);
    }
  }
  </code></pre>