Why play the framework by simply throwing a new Render into the controller class

I am using the game now, here is some code in the controller.class class I do not 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 why the game structure just throws a new Render object? he seems wired to use the "cast" without exception.

+4
source share
2 answers

Playback uses Exceptions to control flow β€” instead of having your methods return something like a Model, they all throw Exceptions. They are not marked with exceptions (e.g. NullPointerException, etc.), which means that you do not need a throws in your method signature.

Many people are scared of the fact that Play throws exceptions similar to this, but actually very fast. There are two reasons for this.

  • Java exception throwing is much faster than it was.
  • If you look at the parent class of RenderJson , you will see that the method called fillInStackTrace() (at least I think it called) was overridden to do nothing - creating this detailed stack trace you get when that something goes wrong, takes a lot of time, but this is usually normal, because exceptions are usually not thrown all the time. With Play, which uses them to control the flow, it was necessary to remove part of the trace generation of the code stack.
+7
source

Version 2.0 seems to no longer use exceptions for control flow: https://github.com/playframework/Play20/wiki/JavaControllers

+3
source

Source: https://habr.com/ru/post/1386253/


All Articles