Coder Social home page Coder Social logo

java-uncheck-exceptions's Introduction

Java Uncheck Exceptions

Problem

In Java language (source code) you can't throw checked exceptions if method doesn't declare that.

Typical solution is to wrap it into RuntimeException, but this way you hide exception type.

There are big production projects that have a method that wrap exception into RuntimeException to avoid declaring checked exceptions in methods:

Goal

Throw checked Exceptions and Throwables without wrapping and without declaring it in method description.

Implementation

From Java bytecode and JVM POV it's totally ok to throw checked exceptions anywhere, see ATHROW opcode documentation.

The only problem is that you can't write Java code that does this because Java compiler won't let you compile it…

So I codegen bytecode that does it with ASM and then create a jar!

API

Library has 0 dependencies, targets Java 6 and provides a single static method:

UncheckExceptions.thr0w(Throwable t);

I highly recommend statically import thr0w method, so it'll look like standard throw:

import static com.artemzin.uncheckexceptions.UncheckExceptions.thr0w;

public static void main(String[] args) { // Note that it doesn't declare `throws Exception`!
    thr0w(new Exception());
}

Download

I haven't published it to Maven Central yet (hopefully will do soon, Gradle publishing is ridiculous).

However you can download jar to play with from Releases page.

Alternatives

Contributing

Project Structure

Project has two modules:

  • uncheck-exceptions-codegen generates bytecode that can throw checked exception as unchecked (has unit tests).
  • uncheck-exceptions-integration-test consumes generated uncheck-exceptions.jar and runs integration tests to make sure library works as expected.

Building

To build project you need Bash and JDK 7+, then run:

ci/build.sh

It'll output something like:


Done!
uncheck-exceptions.jar can be found in build/libs directory.

IDE

You can import project in IntelliJ or your JVM IDE of choice.

Note:

uncheck-exceptions-integration-test might be red in IDE until you run ci/build.sh because it links to jar that needs to be generated first. PRs to wire it properly via Gradle is very welcome!

java-uncheck-exceptions's People

Contributors

artem-zinnatullin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

java-uncheck-exceptions's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.