final class Utils { public final static RuntimeException woBoilerplate(Exception e) { return (e instanceof RuntimeException) ? (RuntimeException)e : new RuntimeException(e); } }
... import static Utils.unchecked; try { doStuff(); } catch(Exception e) { throw woBoilerplate(e); } finally { cleanup(); } On Sat, Aug 15, 2009 at 11:11 AM, Andreas Petersson <andr...@petersson.at>wrote: > > We did a quite large code refactoring regarding Exceptions in my company > some weeks ago. > The codebase is about 2k classes. Before we did the refactoring, we had > a pretty harmful way of dealing exceptions, like this: > > //do not try this at home... > try{ > Connection c = getConn(); > query_data(); > do_resource_cleanup(); > return result; > }catch (SqlException e){ > logger.info("hmm i guess this just failed"); > return ""; > } > > code like this was cluttered all over the source. what happened was that > people using the system thought their updates were successful, when in > fact they were not. > also, we leaked tons of connections and preparedstatements. > > we changed everything to a catch-rethrow as RuntimeException pattern. a > central Servlet filter catches all exceptions,loggs errors to file and > mail, and redirects to a "submit error description" page. > > so the code mostly looks like this now: > > Connection c = null; > try{ > c = getConn(); > return query_data(); > }catch (SqlException e){ > throw new MyRuntimeException("unexpected error calling XXX using > parameters : PPPP " ,e); > }finally{ > do_resource_cleanup(); //check for nulls > } > > this is how it looks like the old jdbc-based data access classes, which > are the majority. the hibernate-based data access classes swallowed > runtimeexceptions, their erronous error handling code was entirely > removed, cutting their code footprint to 1/3, most of the methods are > one-liners now. > > this refactoring affected about 500 classes. most of the work was done > using the excellent Structural Search and Replace tool inside IntelliJ > Idea. prototyping the changes took about 2 days the actual replace was > done in a couple of minutes after we knew what to change. > > > > -- Viktor Klang Rogue Scala-head Blog: klangism.blogspot.com Twttr: viktorklang --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to javaposse@googlegroups.com To unsubscribe from this group, send email to javaposse+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/javaposse?hl=en -~----------~----~----~----~------~----~------~--~---