Mistři v programování na #hovnokod

Java #1929

    /**
     * Throws a {@link RuntimeException} or descendant.
     *
     * If the given exception is kind of unchecked exception (it's a
     * {@code RuntimeExeption} or it's descendant), it's thrown straight.
     * Otherwise new {@code RuntimeException} is created and thrown and the
     * given exception/error is used as a cause of the new exception.
     *
     * <h3>Example:</h3>
     * <pre>
     * public void setup() {
     *   try {
     *     doSomething(); // it could throw NPE or IOE
     *   } catch (Throwable t) {
     *     Utils.throwUncheckedException(t); // in case of NPE throw NPE, in case of IOE throws RE with cause IOE
     *   }
     * }</pre>
     *
     * @param t given exception or error
     * @throws RuntimeException always
     */
    public static void throwUncheckedException(Throwable t) throws RuntimeException {
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        } else {
            throw new RuntimeException(t);
        }
    }

Anonymous,