title: Java: IOException when writing to a ByteArrayOutputStream?
body: <p>Since <code>ByteArrayOutputStream</code> simply writes to memory, an <code>IOException</code> should never occur. However, because of the contract of the <code>OutputStream</code> interface, all stream operations define <code>IOException</code> in their <code>throws</code> clause.</p>
  
  <p>What is the correct way to "handle" this never-occurring <code>IOException</code>? Simply wrap operations in an empty <code>try-catch</code> block? </p>
  
  <p>Or are there any actual situations where <code>ByteArrayOutputStream</code> could throw an exception? </p>
  
  <p>(See also: <a href="https://stackoverflow.com/questions/1045829/how-can-i-handle-an-ioexception-which-i-know-can-never-be-thrown-in-a-safe-and-r">How can I handle an IOException which I know can never be thrown, in a safe and readable manner?</a>)</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As Jon points out, <code>ByteArrayOutputStream</code> doesn't declare a <code>throws</code> clause on the <code>write</code> methods it defines -- however, it inherits <code>write(byte[])</code> from <code>OutputStream</code>, and that one does throw <code>IOEXception</code> (quite odd that <code>BAOS</code> wouldn't override this method, as it could replace the superclass version -- which writes one byte at a time -- with a far more efficient <code>arraycopy</code> call)</p>