Test Exceptions

Sample Exceptions

Locale: [en_US]

This portlet lets you see what uncaught exceptions will look like in your portlet. Select one of the exceptions below in order to throw it.

You can also cause an exception in this portlet by switching to edit or help mode because no handler has been mapped for those modes, even though they are declared as valid in the portlet.xml file.

Here is an ActionURL for an AbstractController that does not implement the action phase. This will both test the default implementation of handleActionRequestInternal (which should throw an exception) and the DispatcherPortlet facility for forwarding an exception during the action phase to the render phase.

 
Google AdSense
Journal Content

package org.springframework.web.portlet.sample;

import java.util.HashMap;
import java.util.Map;

import javax.portlet.PortletException;
import javax.portlet.PortletSecurityException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.springframework.web.portlet.mvc.AbstractController;
import org.springframework.web.portlet.ModelAndView;

public class ExceptionsController extends AbstractController {

    private final static String THROW_PARAMETER = "throw";
   
    public ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception {

        String throwme = request.getParameter(THROW_PARAMETER);

        // build map of throwable exceptions
        Map exceptions = new HashMap();
        exceptions.put(PortletException.class.getName(),
                new PortletException("This is a test"));
        exceptions.put(PortletSecurityException.class.getName(),
                new PortletSecurityException("This is a test"));

        // see if we have been asked to throw something
        if (throwme != null) {
            Exception ex = (Exception)exceptions.get(throwme);
            if (ex != null)
                throw ex;
        }

        // didn't throw anything -- build model and view
        Map model = new HashMap();
        model.put("exceptions", exceptions.keySet());
        return new ModelAndView("exceptions", "model", model);
    }

}