Skip to content
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.

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

Try.withCatch

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)));
		 
}

Success.of / Failure.of

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()));
		
		
	}

Try.runWithCatch

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 / Try with Resources

Try.catchExceptions(FileNotFoundException.class,IOException.class)
				   .init(()->new BufferedReader(new FileReader("file.txt")))
				   .tryWithResources(this::read);

Try with multiple resources

Try.catchExceptions(FileNotFoundException.class,IOException.class)
				   .init(()->Arrays.asList(new BufferedReader(new FileReader("file.txt")),new FileReader("hello")))
				   .tryWithResources(this::read2);

Handling exceptions inside a Stream with AnyM

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]

Reading a file

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);
Clone this wiki locally