<p>To make a Java program as robust as possible it needs to be able to handle <a href="https://www.thoughtco.com/types-of-exceptions-2033910" data-component="link" data-source="inlineLink" data-type="internalLink" data-ordinal="1">exceptions</a>. The compiler does its part by not allowing you to compile a program until it is syntactically correct and can also point out <a href="https://www.thoughtco.com/types-of-exceptions-2033910" data-component="link" data-source="inlineLink" data-type="internalLink" data-ordinal="2">checked exceptions</a> that must be handled. But the exceptions that are likely to cause the most headaches are the ones that appear once the program is running. To help handle these exceptions the <a data-inlink="KAx2liE24Vy68JYQCUZd6w&#61;&#61;" href="https://www.thoughtco.com/what-is-java-2034117" data-component="link" data-source="inlineLink" data-type="internalLink" data-ordinal="3">Java language</a> provide the try-catch-finally blocks.</p><h3>The try Block</h3><p>The <code>try</code> block encases any statements that might cause an exception to occur. For example, if you are reading data from a file using the <code>FileReader</code> class its expected that you handle the <code>IOExceptions</code> associated with using a <code>FileReader</code> object (e.g, <code>FileNotFoundException</code>, <code>IOException</code>). To ensure this happens you can place the statements that deal with creating and using the <code>FileReader</code> object inside a <code>try</code> block:</p><pre> <code> public static void main(String[] args){ FileReader fileInput &#61; null; try { //Open the input file fileInput &#61; new FileReader(&#34;Untitled.txt&#34;); } } </code></pre><p>However, the code is incomplete because in order for the exception to be handled we need a place for it to be caught. This happens in the <code>catch</code> block .</p><h3>The catch Block</h3><p>The <code>catch</code> block(s) provide a place to handle the exception thrown by the statements within a <code>try</code> block. The <code>catch</code> block is defined directly after the <code>try</code> block. It must specify the type of exception it is handling. For example, the <code>FileReader</code> object defined in the code above is capable of throwing a <code>FileNotFoundException</code> or an <code>IOException</code>. We can specify two <code>catch</code> blocks to handle both of those exceptions:</p><pre> <code> public static void main(String[] args){ FileReader fileInput &#61; null; try { //Open the input file fileInput &#61; new FileReader(&#34;Untitled.txt&#34;); } catch(FileNotFoundException ex) { //handle the FileNotFoundException } catch(IOException ex) { //handle the IOException } } </code></pre><p>In the <code>FileNotFoundException</code> <code>catch</code> block we could place code to ask the user to find the file for us and then try to read the file again. In the <code>IOException</code> catch block we might just pass on the I/O error to the user and ask them to try something else. Either way, we have provided a way for the program to catch an exception and handle it in a controlled manner.</p><p>In Java SE 7 it became possible to handled multiple exceptions in one <code>catch</code> block. If the code we wanted to place in the two <code>catch</code> blocks above was exactly the same we could write the code like this instead:</p><pre> <code> public static void main(String[] args){ FileReader fileInput &#61; null; try { //Open the input file fileInput &#61; new FileReader(&#34;Untitled.txt&#34;); } catch(FileNotFoundException | IOException ex) { //handle both exceptions } } </code></pre><p>In order to do a bit of housekeeping as far as resources go, we can add a finally block. After all, we want to release the file we have been reading from once we are finished.</p><h3>The finally Block</h3><p>The statements in the finally block are always executed. This is useful to clean up resources in the event of the try block executing without an exception and in the cases when there is an exception. In both eventualities, we can close the file we have been using. </p><p>The finally block appears directly after the last catch block:</p><pre> <code> public static void main(String[] args){ FileReader fileInput &#61; null; try { //Open the input file fileInput &#61; new FileReader(&#34;Untitled.txt&#34;); } catch(FileNotFoundException | IOException ex) { //handle both exceptions } finally { //We must remember to close streams //Check to see if they are null in case there was an //IO error and they are never initialised if (fileInput !&#61; null) { fileInput.close(); } } } </code></pre>