无法在 java 中捕获自定义异常

Unable to catch custom Exception in java

在我的资源方法中,我将得到以下异常

@Override
    public Response registerByDebitCard(RegistrationRequest registrationRequest,BeanFilter beanFilter) {
        try {


            RegistrationResponse registrationResponse = registrationService.doRegister(registrationRequest, beanFilter);

            return Response.ok(registrationResponse).build();
        } catch (CannotCreateTransactionException e) {
            LOGGER.error("Error Message is :: {}", e.getMessage());
            throw new RegistrationFailureException("MPYR0012", "Due to database technical problem signup failed");
        }
    }

如果我捕获到上述异常,它就会起作用。 我尝试使用我的自定义异常 class 捕获此异常我无法 catch.Below 是我的自定义异常 class.

public class TransactionManagerDBException extends CannotCreateTransactionException {

    /**
     * @param msg
     */
    public TransactionManagerDBException(String msg) {
        super(msg);

    }

    public TransactionManagerDBException(String msg, Throwable cause) {
        super(msg, cause);

    }

}

我的资源方法:

@Override
        public Response registerByDebitCard(RegistrationRequest registrationRequest,BeanFilter beanFilter) {
            try {


                RegistrationResponse registrationResponse = registrationService.doRegister(registrationRequest, beanFilter);

                return Response.ok(registrationResponse).build();
            } catch (TransactionManagerDBException e) {
                LOGGER.error("Error Message is :: {}", e.getMessage());
                throw new RegistrationFailureException("MPYR0012", "Due to database technical problem signup failed");
            }
        }

现在我无法捕捉到这个 exception.Is 他们在我的异常中有什么问题 handling.please 解释我的方法有什么问题

任何帮助将不胜感激!!!!

你的 TransactionManagerDBException IS-A CannotCreateTransactionException。但反之则不然。如果您的 registrationService 确实抛出了 CannotCreateTransactionException,那不是 TransactionManagerDBException

如果您无法更改 doRegister 方法以抛出 TransactionManagerDBException,您只能使用 CannotCreateTransactionException.

的 catch 块