Handling Uncaught Exceptions

Hi,

I want to add a custom exception for uncought exceptions. For example when there occurs ClassCastException exception, in stead of showing that as a dialogue box it should show an error notification with the message as "Application Error occurred. Please check with your administrator. "

Please suggest how to implement this feature.

Thanks,
Saumm

You can define a custom exception handler, see Client-Level Exception Handlers - CUBA Platform. Developer’s Manual

Hi I tried to create a custom exception handler by following the steps in the docs. Unfortunately it’s not working. I kept the handler in web module and the codes are as follows.


mport com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.Frame;
import com.haulmont.cuba.gui.exception.AbstractGenericExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Nullable;

/**
 * Created by Saumm on 8/4/2016.
 */

@Component("cuba_ClassCastExceptionExceptionHandler")
public class ClassCastExceptionExceptionHandler extends AbstractGenericExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(ClassCastExceptionExceptionHandler.class);

    public ClassCastExceptionExceptionHandler(){
        logger.info("Custom exception handler");
    }

    @Override
    protected boolean canHandle(String className, String message, @Nullable Throwable throwable) {
        return true;

    }

    @Override
    protected void doHandle(String className, String message, @Nullable Throwable throwable, WindowManager windowManager) {
        String msg = "Exception Occured";
        windowManager.showNotification(msg, Frame.NotificationType.ERROR_HTML);
    }
}

Please tell me where I am missing in the implementation.

You haven’t defined the class of exception which should be handled.
Do it in the constructor:


    public ClassCastExceptionExceptionHandler(){
        super(ClassCastException.class.getName());
        logger.info("Custom exception handler");
    }

or


    public ClassCastExceptionExceptionHandler(){
        super("java.lang.ClassCastException");
        logger.info("Custom exception handler");
    }