Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Announcing SneakyThrow

DZone's Guide to

Announcing SneakyThrow

Wish there was a shorter way to write code for Checked Exceptions? Try out SneakyThrow, a new open source library that aims at simplifying your code.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Are you tired of writing “catch-pseudo-code” for Java’s Checked Exceptions? And let’s not think about that annoying additional unit test.

Help is on the way.

Let me introduce you to SneakyThrow. Cut back the amount of code you need to write to catch Checked Exceptions thanks to this Java library.

It uses only “legal stuff” — no magic like bytecode manipulation. (I am looking at you, Project Lombok.)

SneakyThrow wraps the Exception into a RuntimeException. And as we all know: We don’t have to catch these types of exceptions. We can leave them as Unchecked Exceptions.

SneakyThrow in Action

How Can I Get It?

Simply copy and paste the library as a dependency into your pom.xml or similar dependency manager of choice:

<dependency>
  <groupId>com.rainerhahnekamp</groupId>
  <artifactId>sneakythrow</artifactId>
  <version>1.0.0</version>
</dependency>


How Can I Use It?

Without SneakyThrow, code with Checked Exceptions looks like this:

URL url;
try {
  url = new URL("https://www.hahnekamp.com");
} catch (MalformedURLException mue) {
  throw new RuntimeException(mue);
}


Thanks to SneakyThrow, your code looks like this:

URL url = sneak(() -> new URL("https://www.hahnekamp.com"));


Great! Does it Also Work With Stream and Lambdas?

Sure thing. Once again, the code without SneakyThrow:

private URL createURL(String url) throws MalformedURLException {
  return new URL(url);
}

Stream
  .of("https://www.hahnekamp.com", "https://www.austria.info")
  .map(url -> {
    try {
      return this.createURL(url);
    } catch (MalformedURLException mue) {
      throw new RuntimeException(mue);
    }
  })  
  .collect(Collectors.toList());


And now, again, much less code with SneakyThrow:

private URL createURL(String url) throws MalformedURLException {
  return new URL(url);
}

Stream
  .of("https://www.hahnekamp.com", "https://www.austria.info")
  .map(sneaked(this::createURL))
  .collect(Collectors.toList());


Please note the difference in sneak and sneaked.

Where Can I Get More Info?

The article "Ignore Exceptions in Java" describes the rationale and the basic approach behind SneakyThrow in more detail.

SneakyThrow is open source and available on GitHub.

Try it out and let me hear how it is going. Feedback of any kind is welcome.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
java ,exception handler ,sneakythrow ,checked exceptions

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}