Skip to content

zleonov/unchecked-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unchecked Java

Java Streams and lambda expressions that are free from checked exceptions side effects.

Overview

Functional Interfaces provided in Java 8+ do not throw checked exceptions. As a consequence handling checked exceptions in lambda expressions can be overly cumbersome and verbose.

For example:

Stream.of("https://www.google.com").map(URL::new);

Must be coded as:

Stream.of("https://www.google.com").map(url -> {
    try {
        return new URL(url);
    } catch (final MalformedURLException e) {
        throw new RuntimeException(e);
    }
});

Besides the extra boiler-plate code, we are also required to obfuscate the original exception by wrapping it in a RuntimeException which adds unnecessary bloat to the stack trace. Using Unchecked Java we can again write concise code afforded to us by lambda expressions, without wrapping checked exceptions in runtime exceptions:

import static software.leonov.common.util.function.CheckedFunction.unchecked;
...
Stream.of("https://www.google.com").map(unchecked(URL::new));

Documentation

Please refer to the Wiki for details, specifications, API examples, and FAQ.

The latest API documentation can be accessed here.

WARNING

Unchecked Java circumvents Java's exception handling mechanisms and can lead to horrible, often very hard to debug errors, when misused.

As Brian Goetz (Java Language Architect) put it:

Just because you don’t like the rules, doesn’t mean its a good idea to take the law into your own hands. Your advice is irresponsible because it places the convenience of the code writer over the far more important considerations of transparency and maintainability of the program.

Please refer to the Safety Guide before using Unchecked Java.

Similar Libraries