I know how to create a reference to a method that has a String
parameter and returns an int
, it\'s:
Function
You can use ET for this. ET is a small Java 8 library for exception conversion/translation.
With ET it looks like this:
// Do this once
ExceptionTranslator et = ET.newConfiguration().done();
...
// if your method returns something
Function f = (t) -> et.withReturningTranslation(() -> myMethod(t));
// if your method returns nothing
Consumer c = (t) -> et.withTranslation(() -> myMethod(t));
ExceptionTranslator
instances are thread safe an can be shared by multiple components. You can configure more specific exception conversion rules (e.g. FooCheckedException -> BarRuntimeException
) if you like.
If no other rules are available, checked exceptions are automatically converted to RuntimeException
.
(Disclaimer: I am the author of ET)