gpt4 book ai didi

java - 无法捕获java中的自定义异常

转载 作者:太空宇宙 更新时间:2023-11-04 12:33:14 24 4
gpt4 key购买 nike

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

@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");
}
}

如果我捕获上述异常,它就可以工作。我尝试使用我无法捕获的自定义异常类来捕获此异常。下面是我的自定义异常类。

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");
}
}

现在我无法捕获这个异常。他们在我的异常处理中是否有任何问题。请解释我的方法有什么问题

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

最佳答案

您的TransactionManagerDBExceptionCannotCreateTransactionException。但反之则不然。如果您的 registrationService 确实抛出 CannotCreateTransactionException,它不是 TransactionManagerDBException

如果无法更改 doRegister 方法以引发 TransactionManagerDBException,则只能使用 CannotCreateTransactionException 来使用 catch block 。

关于java - 无法捕获java中的自定义异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37609121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com