Techcable / SneakyThrows.java

Single-file library for bypassing Java's checked exceptions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SneakyThrows.java

A single-file library to bypass checked exceptions in Java.

Example

This ability results in more composable and clearer code.

Replaces 'wrapper' RuntimeExceptions

try {
    input.read();
} catch (IOException e) {
    /*
     * this wrapper object obscures the true cause of the exception.
     *
     * The message will be blank and the stack trace will point
     * here instead of where the IOException was thrown.
     */
    throw new RuntimeException(e);
}

// Instaed
try {
    input.read();
} catch (IOException e) {
    /*
     * Propagates the exception directly, without constructing a wrapper.
     *
     * The stack trace will point where the exception was originally thrown.
     */
    throw SneakyThrows.sneakyThrow(e);
}

Avoid try/catch with sneakyThrows lambda

/*
 * The try-catch is verbrose and misleading.
 *
 * It makes it appear we are handling or processing the exeption
 * when we are really unconditonally propagating it.
 */
try {
    inputStream.read();
} catch (IOException e) {
    throw Sneakythrows.sneakyThrow(e);
}

// Same thing as above, but much clearer
byte returnValue = SneakyThrow.sneakyThrows(() -> inputStream.read());

License

This project is dual-licensed under the Apache 2.0 and CC0 1.0 licenses.

Importantly, the CC0 allows you to copy this code directy into your project without needing to give attribution or maintain a copy of the original license.

About

Single-file library for bypassing Java's checked exceptions


Languages

Language:Java 100.0%