Exception handling in Jmix 2.x.x

Hello,
I am trying to apply exception handling in my project using Jmix 2.1.1. I looked into the Jmix documentation and there is only this page Exception Handlers :: Jmix Documentation which is related to Jmix 1.5.
Could you, please, tell me how this is done in Jmix 2.1?

Hi Rita,

Assuming that you have an exception like this:

package com.company.onboarding.exception;

public class MyException extends RuntimeException {

    public MyException(String string) {
        super(string);
    }
}

you can create an exception handler for it as follows:

package com.company.onboarding.view;

import com.company.onboarding.exception.MyException;
import io.jmix.flowui.Notifications;
import io.jmix.flowui.exception.AbstractUiExceptionHandler;
import org.springframework.stereotype.Component;

@Component
public class MyExceptionHandler extends AbstractUiExceptionHandler {

    private final Notifications notifications;

    public MyExceptionHandler(Notifications notifications) {
        super(MyException.class.getName());
        this.notifications = notifications;
    }

    @Override
    protected void doHandle(String className, String message, Throwable throwable) {
        notifications.show("My exception occurred: " + throwable.getMessage());
    }
}

Regards,
Konstantin

1 Like

Thanks for your reply.
I have already done the same you have suggested, but the exception is still being handled by the default mechanism provided in Jmix, which is, as far as i understood, the custom unhandled exception dialogs.
Could you tell me, please, am i missing anything?

I cannot find out what you are missing unless I see your code :slight_smile:
Could you provide a test project?

Just in case: make sure you provide the correct exception class name in your handler constructor.

My mistake was that I accidentally exchanged the exception class name with the handler class name in the handler constructor.
It’s all working now. Thanks again Konstantin, for your help.