{"id":6440,"date":"2012-08-27T12:52:50","date_gmt":"2012-08-27T07:22:50","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6440"},"modified":"2012-08-27T17:31:04","modified_gmt":"2012-08-27T12:01:04","slug":"try-catch-with-resources-in-jdk-7","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/try-catch-with-resources-in-jdk-7\/","title":{"rendered":"New Features in Try\/Catch &#8211; JDK 7"},"content":{"rendered":"<p>In our applications we generally make use of try-catch blocks to handle exceptions. Since JDK-7 , It has certain new features. These features are quite good , I must say.<\/p>\n<p style=\"padding-top: 10px;\">\n<p><strong>Some of the new changes in try\/catch bloack are :<\/strong><\/p>\n<p style=\"padding-top: 10px;\">\n<p>1.\u00a0 Multiple exceptions handling in only one catch block.<br \/>\n2.\u00a0 Finally out of scope. (Try-Catch with resources is new feature.)<\/p>\n<p style=\"padding-top: 10px;\">\n<p><strong>1. Catching Multiple Exception Types<\/strong><\/p>\n<p>In our applications , We generally make use of catch block like these :-<\/p>\n<p>[java]<br \/>\ncatch (SQLException ex) {<br \/>\n     logger.log(ex);<br \/>\n     throw ex;<br \/>\ncatch (IOException ex) {<br \/>\n     logger.log(ex);<br \/>\n     throw ex;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p style=\"padding-top: 10px;\">\n<p>We can cleary see that we are maintaining duplicate code for handling exceptions . So , In Java SE 7 there are some modifications that resolves this issue.<\/p>\n<p style=\"padding-top: 10px;\">\n<p><strong>Have a look at following snippet of code :-<\/strong><\/p>\n<p style=\"padding-top: 10px;\">\n<p>[java]<br \/>\ncatch (IOException | SQLException ex) {<br \/>\n   logger.log(ex);<br \/>\n   throw ex;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p style=\"padding-top: 10px;\">\n<p>Here above , We can see that a single catch block is handling multiple exceptions. Isn&#8217;t it cool. \ud83d\ude42<\/p>\n<p style=\"padding-top: 10px;\">\n<p>For more details , Please visit <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/technotes\/guides\/language\/catch-multiple.html\">Oracle-docs for try-catch<\/a><\/p>\n<p style=\"padding-top: 10px;\">\n<p><strong>2. Finally out of scope. (Try-Catch with resources is new feature.)<\/strong><\/p>\n<p>We generally make use of finally block to close thre resources we used in our code.<br \/>\ne.g.<\/p>\n<p>[java]<br \/>\nString readLineFromBlock(String pathOfFile)<br \/>\n                                                     throws IOException {<br \/>\n    BufferedReader br = new BufferedReader(new FileReader(pathOfFile));<br \/>\n    try {<br \/>\n        return br.readLine();<br \/>\n    } finally {<br \/>\n        if (br != null) br.close();<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p style=\"padding-top: 10px;\">\n<p>You can see that in above written snippet of code, We are closing the reader  and handling the exception for the same ,<strong> But In JDK7, <code>finally<\/code> is no longer required<\/strong>. Now in JDK-7 a new feature is added that is , Try-Catch with resources . This is a wonderful feature .<\/p>\n<p style=\"padding-top: 10px;\">\n<p><strong>Just have a look at the syntax :<\/strong><\/p>\n<p>[java]<br \/>\nString readLineFromBlock(String pathOfFile) throws IOException {<br \/>\n     try (BufferedReader br =new BufferedReader(new FileReader(pathOfFile))) {<br \/>\n         return br.readLine();<br \/>\n     }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p style=\"padding-top: 10px;\">\n<p>You can see that in above written snippet of code, We are not closing the reader  and handling the exception for the same , It is being taken care of automatically by JDK-7.<\/p>\n<p style=\"padding-top: 10px;\">\n<p>Now closing the streams , readers n all will not be a headache for you , Need not to to write a extra snippet of code for them &amp; then enclosing them again in try-catch block.<\/p>\n<p style=\"padding-top: 10px;\">\n<p>For more details , Please visit <a href=\"http:\/\/docs.oracle.com\/javase\/tutorial\/essential\/exceptions\/tryResourceClose.html\">try-catch resource<\/a><\/p>\n<p style=\"padding-top: 10px;\">\n<p>These are really good features, Quite helpful . Just give a  try . \ud83d\ude42<\/p>\n<p style=\"padding-top: 10px;\">\n<p>Thanks &amp; Regards,<br \/>\nRobin Sharma,<\/p>\n<p>robin@intelligrape.com,<br \/>\nIntelligrape Software Services.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our applications we generally make use of try-catch blocks to handle exceptions. Since JDK-7 , It has certain new features. These features are quite good , I must say. Some of the new changes in try\/catch bloack are : 1.\u00a0 Multiple exceptions handling in only one catch block. 2.\u00a0 Finally out of scope. (Try-Catch [&hellip;]<\/p>\n","protected":false},"author":54,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[886,887],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6440"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/54"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6440"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6440\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}