Skip to content

cyclops invoke dynamic : ExceptionSoftener

johnmcclean-aol edited this page Feb 24, 2016 · 2 revisions

Cyclops has merged with simple-react. Please update your bookmarks (stars :) ) to https://github.com/aol/cyclops-react

All new develpoment on cyclops occurs in cyclops-react. Older modules are still available in maven central.

screen shot 2016-02-22 at 8 44 42 pm

ExceptionSoftener

The cyclops ExceptionSoftener converts CheckedExceptions into UncheckedExceptions without changing the Exception type. That is, your function or method can still throw IOException, it just no longer needs to declare it.

The JDK functional interfaces don't support CheckedExceptions, so the ExceptionSoftener can prove very useful when working with those.

ExceptionSoftener provides softenXXX methods for all Checked Functional interfaces in jOOλ

Example usage

Throwing a softened exception

throw ExceptionSoftener.throwSoftenedException(new IOException("hello"));

throw ExceptionSoftener.throwSoftenedException(new Exception("hello"));

//doesn't need softened, but will still work
throw ExceptionSoftener.throwSoftenedException(new RuntimeException("hello"));

Softening a Function

ExceptionSoftener.softenFunction(this::load)
                 .apply("input");

public String load(String file) throws IOException{
        throw new IOException();
}

Softening a Supplier

Supplier<String> supplier = ExceptionSoftener.softenSupplier(this::get);
		
assertThat(supplier.get(),equalTo("hello"));

private String get() throws IOException{
		return "hello";
}
Clone this wiki locally