Skip to content

Simple example of rethrowing unchecked exception for checked exceptions to simplify usage in Java Streams.

License

Notifications You must be signed in to change notification settings

skohlmann/rethrow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rethrow

A simple example for transforming checked exceptions into unchecked exceptions for simple use in Java Streams.

Usage

Before

import java.io.IOException;
import java.io.UncheckedIOException;

public class PathContainer implements Iterable<Path> {

    public void copyAllTo(final Path target) {
        forEach(source -> {
            try {
                Files.copy(source, target);
            } catch (final IOException e) {
                throw new UncheckedIOException(e);
            }
        });
    }
}

After

import static de.speexx.rethrow.ThrowingConsumer.rethrowUnchecked;

public class PathContainer implements Iterable<Path> {

    public void copyAllTo(final Path target) {
        forEach(rethrowUnchecked(source -> Files.copy(source, target)));
    }
}

That's all, folks.

Savings:

  • ~25% code & statements
  • 6 lines of code & statements

About

Simple example of rethrowing unchecked exception for checked exceptions to simplify usage in Java Streams.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages