Should I catch Exceptions and return error() or just throw the Exception from my Controller in the Play Framework

playframework

Solution

Your code actually does nothing because PlayFramework will do almost exactly the same thing, i.e. catch any uncaptured exception and call error() to output 500 internal error.

In my project we report any unexpected error, in other words Exceptions by sending email to developer, basically some code like the follows:

public class ErrorHelper extends Controller{
    @Catch(Exception.class) 
    public static void handleException(final Exception e) {
        String errorCode = ErrorHelper.generateErrorCode();
        renderArgs.put("errorCode", errorCode);
        new Job() {
            @Override
            public void doJob() throws Exception {
                Mail.reportError(errorCode, e);
            }
        }.now();
    }
}

And then in your controller you needs to `@With(ErrorHelper.class)`.

Also you can customize your `500.html` file by showing the error code to end user and ask them to use it to call technical support, which, should already received an email if no exception.

Problem

Me and my team are relatively new to the Play! framework (1.2.5), we've create a fair amount of code which looks something similar to this in our Controllers. ``` public static void list() { try { List<ActionModel> actions = ActionModel.loadActions(); render( actions ); } catch ( Exception ex ) { error(); } } ``` The Exception comes from our model layer which we have some custom code in, as opposed to the usual Play Framework models. My question is, can skip the try catch and just throw the Exception out of the controller method? And is that in fact a more correct way to deal with this?

Original source