@ExceptionHandler在Service类中不起作用

时间:2020-09-16 05:55:44

标签: spring-boot exception exceptionhandler

以下是服务类别

@Service
class Test {

    public Object findEmployees1(String id, String dbId) {
        return employeeRepo.findByDatabaseIdAndIdentifier(dbId, id);

    }
    
        public Object findEmployees2(String name, String dbId) {
        Object records = employeeRepo.findByNameAndDatabaseId(name, dbId);

    }
    
    
        @ExceptionHandler(Exception.class)
        public void internalErrorExceptionHandler(Exception ex) {
        LOGGER.error("Log the exception here");
        throw new InternalErrorException(ex.getMessage());
    }
}

我想要在类test中的任何方法中出现任何异常(例如某些SQLException),它会被@ExceptionHandler捕获并记录在那里并重新抛出

我不能将@ExceptionHandler@ControllerAdvice一起使用,因为我没有任何带有@Controller注释的类。我只是在编写一个通用模块以从数据库中获取数据。

执行代码时,@ExceptionHandler下的日志不会被打印,因为它永远不会被调用。 要求是,如果任何服务类方法中有任何异常而没有在每个方法中使用单独的try catch语句,则记录并抛出一个公共异常。

0 个答案:

没有答案