{"id":1078,"date":"2011-07-25T11:58:31","date_gmt":"2011-07-25T10:58:31","guid":{"rendered":"http:\/\/www.thomaskeller.biz\/blog\/?p=1078"},"modified":"2011-07-25T11:59:31","modified_gmt":"2011-07-25T10:59:31","slug":"exception-chaining-in-java","status":"publish","type":"post","link":"https:\/\/www.thomaskeller.biz\/blog\/2011\/07\/25\/exception-chaining-in-java\/","title":{"rendered":"Exception chaining in Java"},"content":{"rendered":"<p>If you catch and rethrow exceptions in Java, you probably know about exception chaining already: You simply give the exception you &#8220;wrap&#8221; as second argument to your Exception like this<\/p>\n<p>    try { &#8230; }<br \/>\n    catch (Exception e) {<br \/>\n      throw new CustomException(&#8220;something went wrong&#8221;, e);<br \/>\n    }<\/p>\n<p>and if you look at the stack trace of the newly thrown exception, the original one is listed as &#8220;Caused by:&#8221;. Now today I had the rather &#8220;usual&#8221; use case of cleanup up a failing action and the cleanup itself was able to throw as well. So I had two causing exceptions and I wanted to conserve both of them, including their complete cause chain, in a new exception. Consider the following example:<\/p>\n<p>    try { &#8230; }<br \/>\n    catch (Exception e1) {<br \/>\n      try { &#8230; }<br \/>\n      catch (Exception e2) {<br \/>\n         \/\/ how to transport e1 and e2 in a new exception here?!<br \/>\n      }<br \/>\n      throw e1;<br \/>\n    }<\/p>\n<p>My idea here was to somehow tack the exception chain of `e1` onto the exception chain of `e2`, but Java offered no solution for this. So I hunted for my own one:<\/p>\n<p>    public static class ChainedException extends Exception {<br \/>\n      public ChainedException(String msg, Throwable cause) {<br \/>\n        super(msg, cause);<br \/>\n      }<br \/>\n      public void appendRootCause(Throwable cause) {<br \/>\n        Throwable parent = this;<br \/>\n        while (parent.getCause() != null) {<br \/>\n          parent = parent.getCause();<br \/>\n        }<br \/>\n        parent.initCause(cause);<br \/>\n      }<br \/>\n    }<\/p>\n<p>Now I only had to base the exceptions I actually want to chain on `ChainedException` and was able to do this (in fact I based all of them on this class):<\/p>\n<p>    try { &#8230; }<br \/>\n    catch (ChainedException e1) {<br \/>\n      try { &#8230; }<br \/>\n      catch (ChainedException e2) {<br \/>\n        e2.appendRootCause(e1);<br \/>\n        throw new ChainedException(&#8220;cleanup failed&#8221;, e2);<br \/>\n      }<br \/>\n      throw e1;<br \/>\n    }<\/p>\n<p>Try it out yourself &#8211; you&#8217;ll see the trace of `e1` at the bottom of the cause chain of `e2`. Quite nice, eh?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you catch and rethrow exceptions in Java, you probably know about exception chaining already: You simply give the exception you &#8220;wrap&#8221; as second argument to your Exception like this try { &#8230; } catch (Exception e) { throw new CustomException(&#8220;something went wrong&#8221;, e); } and if you look at the stack trace of the &hellip; <a href=\"https:\/\/www.thomaskeller.biz\/blog\/2011\/07\/25\/exception-chaining-in-java\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Exception chaining in Java<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,25],"tags":[],"class_list":["post-1078","post","type-post","status-publish","format-standard","hentry","category-coding","category-tips"],"_links":{"self":[{"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/posts\/1078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/comments?post=1078"}],"version-history":[{"count":4,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/posts\/1078\/revisions"}],"predecessor-version":[{"id":1082,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/posts\/1078\/revisions\/1082"}],"wp:attachment":[{"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/media?parent=1078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/categories?post=1078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thomaskeller.biz\/blog\/wp-json\/wp\/v2\/tags?post=1078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}