Skip to content

micheljung/nocatch

Repository files navigation

NoCatch

Get rid of annoying checked exceptions!

Usage

Instead of:

try {
  URL url = new URL("http://www.github.com");
  // Other code you really don't want in this try-block
} catch (MalformedURLException e) {
  throw new RuntimeException(e);
}

(or even worse, propagating the exception up your stack)

Just write:

import static com.github.nocatch.NoCatch.noCatch;
...
URL url = noCatch(() -> new URL("http://www.github.com"));

And any checked exception will automatically be wrapped into a runtime exception (NoCatchException). Woa!

Dependency

Gradle:

dependencies {
  compile 'io.github.micheljung:nocatch:2.0'
}

Custom wrapper exception

You can specify your own wrapper exception just like so:

// Throws WrapperException instead of NoCatchException
URL url = noCatch(() -> new URL(";"), WrapperException.class);

Make sure your exception has a contructor like public WrapperException(Throwable cause).

Global custom wrapper exception

Want to specify your own wrapper exception globally? No problem:

NoCatch.setDefaultWrapperException(RuntimeException.class);

// Throws RuntimeException
noCatch(() -> new URL(";"));

About

Get rid of annoying checked exceptions!

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages