-
Notifications
You must be signed in to change notification settings - Fork 51
Try examples
johnmcclean-aol edited this page Feb 24, 2016
·
4 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.

Execute a code block and handle specified exceptions from micro-transactions
public <Ex extends Throwable> Try<R,Ex> execute(T input,Class<Ex> classes){
return Try.withCatch( ()->transactionTemplate.execute(status-> transaction.apply(input)),classes);
}
# Try.withCatch 2
Execute a code block and handle all exceptions from micro-transactions
public Try<R,Throwable> execute(T input){
return Try.withCatch( ()->transactionTemplate.execute(status-> transaction.apply(input)));
}
Referentially transparent exception handling, from micro-reactive
default<K,V> Try<Boolean,MissingPipeException> enqueue(K key,V value){
Optional<Adapter<V>> queue = Pipes.get(key);
queue.map(adapter -> adapter.offer(value));
return queue.isPresent() ? Success.of(true) :
Failure.of(new MissingPipeException("Missing queue for key : " + key.toString()));
}
Execute a code block with no success value (Try<Void,Exception).
Try.runWithCatch(this::exceptional,IOException.class)
.onFail(System.out::println);
//Try[IOEXception]
private void exceptional() throws IOException{
throw new IOException();
}
Try.catchExceptions(FileNotFoundException.class,IOException.class)
.init(()->new BufferedReader(new FileReader("file.txt")))
.tryWithResources(this::read);
Try.catchExceptions(FileNotFoundException.class,IOException.class)
.init(()->Arrays.asList(new BufferedReader(new FileReader("file.txt")),new FileReader("hello")))
.tryWithResources(this::read2);
List<Integer> list = AnyM.fromStream(Stream.of(1,2,3))
.<Integer>bind(i -> Try.withCatch( ()-> { if(i==1) { throw new RuntimeException();} else{ return i+2; } }) )
.asSequence()
.toList();
//List[4,5]
Try.catchExceptions(NullPointerException.class,IOException.class)
.init(() -> new BufferedReader(new FileReader("file.txt")))
.tryWithResources(this::read)
.onFail(NullPointerException.class, System.err::println)
.onFail(System.err::println);