Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why play framework simply throw a new Render in controller class

i'm using play now , here is some code in the controller.class i don't understand :

/**
 * Render a 200 OK application/json response
 * @param jsonString The JSON string
 */
protected static void renderJSON(String jsonString) {
    throw new RenderJson(jsonString);
}

is there any important reason make play framework simply throw a new Render object ? it seems wired to use "throw" without an exception.

like image 523
WoooHaaaa Avatar asked Dec 28 '22 09:12

WoooHaaaa


1 Answers

Play uses Exceptions for flow control - rather than your methods returning something like a Model, they all throw Exceptions. They are unchecked Exceptions (like NullPointerException, etc) meaning that you don't need a throws clause on your method signature.

A lot of people are scared by the fact that Play throws Exceptions like this, but it's actually very fast. This is for two reasons

  1. Java Exception throwing is a lot quicker than it was
  2. if you look at the parent class(es) of RenderJson you'll see that a method called fillInStackTrace() (at least I think that's what it's called) has been overridden to not do anything - creating that detailed stack trace you get when something goes wrong takes a lot of time, but normally that's ok because Exceptions aren't normally thrown all the time. With Play using them for flow control, it was necessary to remove the stack trace generation part of the code.
like image 194
Rich Avatar answered Jan 03 '23 07:01

Rich