Currently, I am creating a library that will be responsible for handling exceptions in a project. This library is already being imported into the main project, and has a GlobalExceptionHandler in addition to several custom error types defined.
In a certain part of the code I have:
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Resource
private MessageSource messageSource;
@ExceptionHandler(Exception.class)
private ResponseEntity<Object> handleGeneral(Exception e, WebRequest request) {
String message = "";
if (e.getClass().isAssignableFrom(UndeclaredThrowableException.class)) {
I am managing to throw an Exception and it is being caught by the global handler, however when it reaches this IF
e.getClass().isAssignableFrom(UndeclaredThrowableException.class)
, it is giving the following error:
java.lang.ClassCastException: class java.lang.Exception cannot be cast to class java.lang.reflect.UndeclaredThrowableException (java.lang.Exception and java.lang.reflect.UndeclaredThrowableException are in module java.base of loader 'bootstrap')
Can anyone help me? This piece of code works correctly when exception handling is done directly in the project. I have no idea how to proceed.
Note: e.getClass().isAssignableFrom(UndeclaredThrowableException.class) == true
, according to the intellij debugger, but from what I’ve been researching, isAssignableFrom tries to perform a cast and then the error appears.
I’ve already tried debugging a project and researching this error in other situations but I didn’t find anything that helped me.
Vitor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.