Skip to content

enhancement: allow Java8 lambda steps to throw checked Exceptions #1001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RichardBradley opened this issue May 3, 2016 · 7 comments
Closed
Labels
⚡ enhancement Request for new functionality

Comments

@RichardBradley
Copy link
Contributor

Any steps declared with the Java8 lambda syntax may not throw checked Exceptions:

    // Compile error: Unhandled exception: IOException
    Given("^I have a file with some text$", () -> {
        Files.write("my setup", new File("example.txt"), Charsets.UTF_8);
    });

It would be more convenient for developers if they were declared as "throws Exception"; the runtime machinery all copes with it fine, and it avoids lots of "catch(e) { throw new RuntimeException(e) }" noise.

This should be a simple change to the interface declarations in StepdefBody

@aslakhellesoy aslakhellesoy added Java ⚡ enhancement Request for new functionality labels Aug 30, 2016
@leodutra
Copy link

Any temporary replacement for this? Any special RuntimeException to be thrown?

@RichardBradley
Copy link
Contributor Author

There is an easy workaround for now: catch all your checked exceptions and rethrow as RuntimeException:

Given("^I have a file with some text$", () -> {
    try {
        Files.write("my setup", new File("example.txt"), Charsets.UTF_8);
    } catch(e) { 
        throw new RuntimeException(e) 
    }
});

@christianhujer
Copy link

I guess the solution is to add throws Exception to all accept() methods of the A* interfaces in src/main/java/cucumber/api/java8/StepdefBody.java. And I agree it would remove a hell lot of noise.

@renannprado
Copy link

renannprado commented Mar 29, 2017

any news on this?
I'm having exact same problem.

@RichardBradley
Copy link
Contributor Author

No-one has stepped up to implement it yet. I would hope that a well written PR fix would be accepted, but I can't speak for the project maintainers. I don't plan to implement it myself in the short / medium term.

@RichardBradley
Copy link
Contributor Author

Thanks!

@lock
Copy link

lock bot commented Oct 25, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
⚡ enhancement Request for new functionality
Projects
None yet
Development

No branches or pull requests

6 participants