I was looking for a solution like this:
(in more detail: for some maven plugin running different build code in several phases/goals):
void foo() {
execCode( () -> doBar() ) ;
execCode( () -> new File("/baz.txt").create() ) ;
execCode( () -> {
System.out.println( "test" ) ;
throw new MojoFailureException("problem 1") ;
}) ;
}
void execCode( Code code ) throws MojoFailureException {
try {
// ... some common initialization / param parsing here ...
code.run() ;
} catch( Exception e ) {
// ... common exception handling throwing e.g. MojoFailureException ...
}
}
supporting (primarily (E), (G) and (S) below):
- (E): the code blocks throwing any
java.lang.Exception
- (V): no in or out values/objects needed
- (S): short, easy to read/write code (minimal clutter)
- also avoiding IDE warnings and dealing with it (e.g. via
@Ignore
for unused generic type parameters)
- also avoiding IDE warnings and dealing with it (e.g. via
- (8): Java 8+ support
- (L): no further lib/jar dependencies, if possible (very common ones like Google Guava or Apache Commons would be fine as well)
- (G): generic context (e.g. some build tasks) where it is fine to just know, that something should/will be done, but the proper exception handling and other stuff (common to each call) should be encapsulated