Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm looking through the Java source code, and have found something interesting which is indicative of the <code>IOException</code> reason. <code>InputStream</code> <strong>is an abstract class</strong>. We therefore can't predict the kind of input which will be closed, so it's good to keep information flowing. </p> <p>Whatever code uses that input stream needs to be able to throw an <code>IOException</code>, because there is a chance that closing the input stream may fail. If it fails, whatever's using the implementation needs to know about it, because there's a good chance it needs to be handled. </p> <p>It's important to understand the layout of the Exception structure in Java. Every exception, of course, extends <code>Exception</code>. However, there are also broader categories of exceptions: <code>java.lang.IOException</code> is one of these, and covers all possible input/output exceptions. When we say there has been an I/O error, we're referencing anything which falls under <code>IOException</code>. As a result, many exceptions extends this one, e.g. <code>FileNotFoundException</code>, <code>EOFException</code>, etc. as it's important to have a broad, overarching exception to manage these.</p> <p>As a result, any IO class will need to be able to throw <em>any</em> of the various IOExceptions on close. <code>close()</code> therefore must throw <code>IOException</code> - this gives its implementation the ability to throw any of the extensions of <code>IOException</code> as well. This is why <code>close()</code> throws <code>IOException</code> - exceptions are inherited, and you need to be able to any of the IOExceptions when closing a stream.</p> <hr> <p>Here are a couple scenarios worth noting:</p> <ul> <li>You can't close an IOStream twice, though this generally doesn't throw an exception if you do</li> <li>The content becomes inaccessible (e.g. a disk was unmounted) (The <code>close()</code> is actually critical for the operating system, as it needs to have an indicator of when the file is no longer busy)</li> <li>The generic source has closed</li> <li>Any generic failure not covered by all other subclasses of <code>IOException</code> (e.g. <code>FileNotFoundException</code>)</li> </ul> <p>You can check what caused an <code>IOException</code> by running <code>Exception.getMessage()</code>. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload