tag:blogger.com,1999:blog-4146794174400139442.post993050870736300041..comments2023-07-03T07:20:53.252-04:00Comments on Whiteknight's World: Software Anti-Pattern: The Exception RepeaterWhiteknighthttp://www.blogger.com/profile/16207472474429254890noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-4146794174400139442.post-77797528755454794622010-02-16T12:27:27.143-05:002010-02-16T12:27:27.143-05:00A coworker of mine justifies this as documentation...A coworker of mine justifies this as documentation that the code might throw an exception. He says it's good practice to indicate to future programmers that you knew about possible exceptions but decided explicitly not to deal with them.<br /><br />I'm not entirely convinced, because my assumption is that any code can throw exceptions at any time. Which means you have to put try/catch everywhere?Careyhttps://www.blogger.com/profile/10316232481882531170noreply@blogger.comtag:blogger.com,1999:blog-4146794174400139442.post-48713105368978054422010-02-15T19:10:08.864-05:002010-02-15T19:10:08.864-05:00Yes, you're right. re-throwing isn't a bad...Yes, you're right. re-throwing isn't a bad thing, so long as you make som kind of change. Throwing the exact same exception in the exact same way is never usefulWhiteknighthttps://www.blogger.com/profile/16207472474429254890noreply@blogger.comtag:blogger.com,1999:blog-4146794174400139442.post-51285561375177515772010-02-15T18:52:24.577-05:002010-02-15T18:52:24.577-05:00As written here, it obviously has no purpose at al...As written here, it obviously has no purpose at all. But slightly modified, I can think of at least two cases where I use it.<br /><br />1. Converting checked exception to unchecked -- applicable only to languages distinguishing checked and unchecked exceptions.<br /><br />public void validateData() {<br /> try {<br /> doValidateData(); // throws Exception<br /> } catch (Exception e) {<br /> throw RuntimeException(e);<br /> }<br />}<br /><br />2. Logging or some other exception processing.<br /><br />public void validateData() throws Exception {<br /> try {<br /> doValidateData(); // throws Exception<br /> } catch (Exception e) {<br /> log.warn("failed", e);<br /> throw e;<br /> }<br />}<br /><br />The advantage is very little, but it is there -- separating exception processing from "real meat". Not extremely important, but I kinda like it this way.Ladislav Thonnoreply@blogger.com