{"id":1638,"date":"2011-03-14T02:09:59","date_gmt":"2011-03-13T20:39:59","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1638"},"modified":"2011-03-14T02:09:59","modified_gmt":"2011-03-13T20:39:59","slug":"exception-chaining","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/java\/exception-chaining\/","title":{"rendered":"Exception chaining in Java"},"content":{"rendered":"<p><strong>Exception chaining <\/strong>is very powerful feature supported by the JDK but most of the developers are not able to use the advantage of Exception chaining in their application. Most of the developers have also seen the code in their existing project but unable to identify the advantage of exception chaining.<\/p>\n<p><strong>Definition:<\/strong> When one exception causes another exception then the second exception must print the log of the first exception also.<\/p>\n<figure id=\"attachment_1639\" aria-describedby=\"caption-attachment-1639\" style=\"width: 367px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Exception-Chaining.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1639\" title=\"Exception Chaining\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Exception-Chaining.png?resize=367%2C165&#038;ssl=1\" alt=\"Exception Chaining\" width=\"367\" height=\"165\" \/><\/a><figcaption id=\"caption-attachment-1639\" class=\"wp-caption-text\">Exception Chaining<\/figcaption><\/figure>\n<p><!--more-->Consider below example:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage com.G2.Exception;\n\npublic class ExceptionTest {\n\n\tstatic int arr&#x5B;] = { 11, 22, 33 };\n\n\tpublic static void main(String&#x5B;] args) {\n\n\t\ttry {\n\t\t\tSystem.out.println(arr&#x5B;4]);\n\t\t} catch (ArrayIndexOutOfBoundsException ae) {\n\t\t\ttry {\n\t\t\t\tMyCustomException e = new MyCustomException(&quot;Exception Occured&quot;);\n\t\t\t\tthrow e;\n\n\t\t\t} catch (MyCustomException e) {\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t\t}\n\t}\n}\n\nclass MyCustomException extends Exception {\n\tpublic MyCustomException(String msg) {\n\t\tsuper(msg);\n\t}\n}\n<\/pre>\n<p>Output :<\/p>\n<blockquote><p>com.G2.Exception.MyCustomException: Exception Occured<br \/>\nat com.G2.Exception.ExceptionTest.main(ExceptionTest.java:13)<\/p><\/blockquote>\n<p>You can see the poor programming practice from the above java code.<br \/>\nActual reason of the exception is &#8220;<strong>ArrayIndexOutOfBoundsException<\/strong>&#8220;\u009d but it is printing the logs of the last exception.<br \/>\nA cause can be associated with a throwable in two ways: \u00a0via a constructor that takes the cause as an argument, or via the<strong> initCause(Throwable)<\/strong> method.<br \/>\n<strong>Solution 1:<\/strong><br \/>\nFirst solution is that, we can use the method <strong>initcause()<\/strong> Method of class Throwable to retain the cause of the exception.<\/p>\n<pre class=\"brush: java; highlight: [14]; title: ; notranslate\" title=\"\">\npackage com.G2.Exception;\n\npublic class ExceptionTest {\n\n\tstatic int arr&#x5B;] = { 11, 22, 33 };\n\n\tpublic static void main(String&#x5B;] args) {\n\n\t\ttry {\n\t\t\tSystem.out.println(arr&#x5B;4]);\n\t\t} catch (ArrayIndexOutOfBoundsException ae) {\n\t\t\ttry {\n\t\t\t\tMyCustomException e = new MyCustomException(&quot;Exception Occured&quot;);\n\t\t\t\te.initCause(ae);\n\t\t\t\tthrow e;\n\n\t\t\t} catch (MyCustomException e) {\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t\t}\n\t}\n}\n\nclass MyCustomException extends Exception {\n\tpublic MyCustomException(String msg) {\n\t\tsuper(msg);\n\t}\n}\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<blockquote><p>com.G2.Exception.MyCustomException: Exception Occured<br \/>\nat com.G2.Exception.ExceptionTest.main(ExceptionTest.java:13)<br \/>\nCaused by: java.lang.ArrayIndexOutOfBoundsException: 4<br \/>\nat com.G2.Exception.ExceptionTest.main(ExceptionTest.java:10)<\/p><\/blockquote>\n<p><strong>Solution 2:<\/strong><\/p>\n<p><strong> <\/strong>In the constructor of the MyCustomException, pass the object of the <strong>Throwable<\/strong>.<\/p>\n<pre class=\"brush: java; highlight: [13]; title: ; notranslate\" title=\"\">\npackage com.G2.Exception;\n\npublic class ExceptionTest {\n\n\tstatic int arr&#x5B;] = { 11, 22, 33 };\n\n\tpublic static void main(String&#x5B;] args) {\n\n\t\ttry {\n\t\t\tSystem.out.println(arr&#x5B;4]);\n\t\t} catch (ArrayIndexOutOfBoundsException ae) {\n\t\t\ttry {\n\t\t\t\tMyCustomException e = new MyCustomException(&quot;Exception Occured&quot;,ae);\n\t\t\t\tthrow e;\n\n\t\t\t} catch (MyCustomException e) {\n\t\t\t\te.printStackTrace();\n\t\t\t}\n\t\t}\n\t}\n}\n\nclass MyCustomException extends Exception {\n\tpublic MyCustomException(String msg,Throwable t) {\n\t\tsuper(msg,t);\n\t}\n}\n<\/pre>\n<p>Following methods are supoorted by the java to support the exception chaining:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nThrowable getCause()\nThrowable initCause(Throwable)\nThrowable(String, Throwable)\nThrowable(Throwable)\nThrowable printStackTrace();\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Concept of Exception chaining with example in JAVA<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[94],"class_list":["post-1638","post","type-post","status-publish","format-standard","hentry","category-java","tag-exception"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2009,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/exception-and-error-handling-framework-in-java\/","url_meta":{"origin":1638,"position":0},"title":"Exception and Error handling framework in Java","author":"Jitendra","date":"April 15, 2011","format":false,"excerpt":"Explain the concept of Exception and Error handling framework in JAVA.","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"JAVA Exception Framework","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/04\/JAVA-Exception-Framework.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2017,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-j2ee-interview-questions-1\/","url_meta":{"origin":1638,"position":1},"title":"Java &#8211; J2EE Interview Questions &#8211; 1","author":"Jitendra","date":"April 15, 2011","format":false,"excerpt":"JAVA - J2EE Interview Questions - 1, JAVA - J2EE Interview Questions - 1,custom tag JSP, Externalization , serialVersionUID, difference between interface and abstract class, iterate HashMap","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":738,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/checked-unchecked-exception\/","url_meta":{"origin":1638,"position":2},"title":"Checked and Unchecked Exception","author":"Jitendra","date":"August 7, 2010","format":false,"excerpt":"Explains the concept of checked exception and unchecked exception in JAVA.","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6101,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/limitless-chaining-of-continuation-object-in-salesforce\/","url_meta":{"origin":1638,"position":3},"title":"Limitless Chaining of Continuation object in Salesforce","author":"Jitendra","date":"May 31, 2017","format":false,"excerpt":"How to create a Continuation Server in Salesforce with the help of JavaScript remoting and Continuation Object","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Chaining of Continuation Object in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Object-GIF.gif?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Object-GIF.gif?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Object-GIF.gif?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Object-GIF.gif?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Object-GIF.gif?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/05\/Continuation-Object-GIF.gif?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":936,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jdbc-example-with-microsoft-access-in-swing\/","url_meta":{"origin":1638,"position":4},"title":"JDBC Example with Microsoft Access in Swing, Next and Previous navigation","author":"Jitendra","date":"August 31, 2010","format":false,"excerpt":"JDBC - Java Database Connectivity example in Swing","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":979,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/invoke-method-using-reflection-in-java\/","url_meta":{"origin":1638,"position":5},"title":"Invoke method using reflection in JAVA","author":"Jitendra","date":"September 6, 2010","format":false,"excerpt":"Invoke method using reflection in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1638"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1638\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}