Exception handling with inheritance

45 views
Skip to first unread message

Andrea Asta

unread,
May 19, 2016, 3:32:08 AM5/19/16
to sparkjava
Is it possibile to use Spark.exception and set a "high-level" exception to catch also its children?

For example if I do exception(Exception.class... catch every exception even if of a subclass.

Thanks

Andrea Asta

unread,
Mar 29, 2017, 6:31:15 AM3/29/17
to sparkjava
Any news on this?
Thanks

Andrea Asta

unread,
Mar 29, 2017, 9:48:44 AM3/29/17
to sparkjava
Sorry it's working with the current version.

Thanks

Il giorno giovedì 19 maggio 2016 09:32:08 UTC+2, Andrea Asta ha scritto:

domin...@gmail.com

unread,
Mar 30, 2017, 6:05:43 AM3/30/17
to sparkjava
This is already the case. Having an Exception Handler for Exception.class will catch Exception.class and any class extending Exception.

Eg. Your Route throws a RuntimeException. You have an Exception Handler catching Exception.class. RuntimeException extends Exception and is handled. Happy days!

get("/client-error", (request, response) -> {
   
throw new RuntimeException("Forced RuntimeException");
});


exception
(Exception.class, (exception, request, response) -> {
    response
.status(400);
    response
.body("This handler catches all exceptions.");
});

Reply all
Reply to author
Forward
0 new messages