How to handle throw exceptions inside finally block in java
<p>In java, it is not recommended to throw exceptions inside <code>finally</code> section in <code>try-chatch</code> block due to hide the propagation of any unhandled <code>throwable</code> which was thrown in the <code>try</code> or <code>catch</code> block. This practice is a <code>blocker</code> level violation according to default sonar profile.</p>

<blockquote>
  <p>Sonar Error: Remove this throw statement from this finally block.</p>
</blockquote>

<p>Please consider the following code snippet.</p>

<p>e.g.: close input stream inside the finally block, and handle possible exceptions might be occurred when closing the stream.</p>

<pre class="lang-java prettyprint-override"><code>    public void upload(File file) {
        ChannelSftp c = (ChannelSftp) channel;
        BufferedInputStream bis = new BufferedInputStream(file.toInputStream());
        try {
            String uploadLocation = Files.simplifyPath(this.fileLocation + "/" + file.getName());
            c.put(bis, uploadLocation);
        } catch (SftpException e) {
            throw new IllegalTargetException("Error occurred while uploading " + e.getMessage());
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                throw new UnsupportedOperationException("Exception occurred while closing Input stream " + e.getMessage());
            }
        }
    }
</code></pre>

<p>It would be grateful if you can show the conventional way of handling these situations.</p>