How VRaptor3 handle Exception in Controller Methods

Hi, All

I have this situation,
in my controller layer, they may throw exceptions, and I want to handle them in one place, for example, I want to log the exception details and forward the request to a jsp page to display some info to our clients.

is there any ways to satisfy my request?

pls be kindly reply this top in English or Chinese~thanks

you can create an interceptor to handle exceptions

http://guj.com.br/posts/list/136307.java

Hi haiker. You are using the Vraptor3 or other version?

Do you need handle exceptions to display a more friendly error page for users or only for logging?

There are a issue at github (http://github.com/caelum/vraptor/issues/#issue/158) to create a pretty exception handler. See this page too: http://guj.com.br/posts/list/30/136307.java

So if you only need to logging the error and return to a generic page, you can use the error-page into your web.xml and create a simple interceptor to generate your log. See bellow.

[code]@Intercepts
@RequestScoped
public class MyLoggingInterceptor implements Interceptor {

@Override
public boolean accepts(ResourceMethod method) {
    return true; // intercept all requests
}

@Override
public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance)
    throws InterceptionException {
     
    try {
        stack.next(method, resourceInstance);
    } catch (Exception e) {
        // my log code
        // you can only print the log message or send by email, etc
    }
}

}[/code]

Best regards

thank you all for your kindly response.

I like vraptor, it makes things easy~ :stuck_out_tongue: