1

I am trying to follow the sparkjava exception handling example located here, without success: http://sparkjava.com/documentation.html#exception-mapping. It appears the code they posted isn't quite right? I was able to fix one of the posted methods so that it compiles. The method on the documentation page which doesn't compile was:

get("/throwexception", (request, response) -> {
    throw new NotFoundException();
});

I changed to this code and it compiles:

    get(new Route("/throwexception") {
        @Override
        public Object handle(Request request, Response response)  {
            throw new IllegalArgumentException();
        }
    });

However, I am unable to get this method to compile. What is wrong? I am using java 8 and IntelliJ community edition 15.0.2 to compile. My java module is set to language level 8. Here is the suspect method:

    exception(Exception.class, (e, request, response) -> {
        //TODO: implement this after it compiles.
    });

Here is the error I get from the compiler:

Error:(83, 9) java: cannot find symbol
  symbol:   method exception(java.lang.Class<java.lang.Exception>,(e,request[...]->{ })
  location: class org.me.JournalController

To confirm that I really am using java 8, this example using a lambda expression does compile:

public class Lambdas {
    public static void main(String[] args) throws Exception {
        new Lambdas().start();
        Thread.sleep(1000);
    }

    public void start(){
        Interface f = () -> System.out.println("test");
    }
}

3 Answers 3

1

The original code uses lambda expressions, you should use Java 8 to compile it.

Sign up to request clarification or add additional context in comments.

6 Comments

I believe I'm using java jdk 1.8.0_66. I can see in the project configuration this is what I have selected. Also, my module has language level 8. Could it be something else?
java version "1.8.0_66" Java(TM) SE Runtime Environment (build 1.8.0_66-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)
do you have import static spark.Spark.*; at the beginning of your file?
sorry, I'm out of ideas :(
I updated the question. I tried a class with a lambda expression and it does compile ... I suspect you're onto something ... I probably don't have spark configured correctly. Not sure what I might be missing. I'll add my maven code.
|
1

My maven file had sparkjava dependency listed twice. I think my IDE automatically imported 1.1.1 for me at some point and I didn't realize it. I had 1.1.1 and 2.3. When I removed the outdated 1.1.1 sparkjava dependency then everything works as expected.

Comments

0

You can extract your throw statement into a function:

get("/throwexception", () -> BlowUp());

(snip)

public static void BlowUp()
{
    throw new NotFoundException();
});

1 Comment

I had that originally. I modified to try to fix the compilation error which was caused by maven dependency problems. Thanks for the suggestion, though ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.