【问题标题】:How to catch non-MVC and non-REST exceptions in Spring Boot如何在 Spring Boot 中捕获非 MVC 和非 REST 异常
【发布时间】:2018-01-24 03:50:19
【问题描述】:

我已经能够找到无数关于如何在 Spring MVC 或 Spring REST 中捕获未处理异常的教程,但我想知道的是如何在完全不使用 Spring Web 框架的情况下捕获未处理异常 .

我正在编写一个没有 web 组件的应用程序,我不会只为异常处理而导入 Spring Web。

@Service 抛出未处理的异常时,我需要捕获它,以便将其正确记录到Raygun

例如,考虑一个故意抛出未捕获异常的服务中的这个方法:

@Scheduled(fixedDelay = 100)
public void doSomething() {
    throw new RuntimeException("Uh oh!");
}

它的输出将是:

2017-08-16 00:19:40.202 ERROR 91168 --- [pool-1-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task.

java.lang.RuntimeException: Uh oh!
    at com.mitchtalmadge.example.ExampleService.doSomething(ClassSyncService.java:48) ~[classes/:na]
    at com.mitchtalmadge.example.ExampleService$$FastClassBySpringCGLIB$$1dd464d8.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:669)
    ...

我如何捕捉到它?

有没有简单的方法可以做到这一点?

【问题讨论】:

  • 为什么不使用try-catch
  • 您能否更详细地解释“@Service 引发未处理的异常”的含义?谁调用了抛出异常的代码?
  • @MehrajMalik Try-catch 在哪里?围绕每个服务中的每个方法?未捕获异常处理程序的意义在于,它可以捕获您使用 try-catch 未预料到的异常。
  • @Oleg 一种情况是 Scheduled 方法,但还有其他地方可以调用它们。这个应用程序尤其是一个 Discord 机器人,因此当收到事件时,例如用户发布消息,它会调用各种服务上的方法。当他们抛出异常时,它所做的只是打印到控制台——我从来不知道。
  • @Oleg 我在问题中添加了一个示例。

标签: java spring raygun


【解决方案1】:

你可以在spirngframework中使用aop,首先你应该配置aop配置。

<bean id="aspect" class="com.zhuyiren.Main"/>

<aop:config>
    <aop:aspect ref="aspect">
        <aop:after-throwing method="after" throwing="ex" pointcut="execution(* com.zhuyiren.service..*.*(..)),args(ex)"/>
    </aop:aspect>
</aop:config>

然后你应该声明一个aspect 类的方法被命名为after

public class Main {

public void after(JoinPoint point,Throwable ex){
    System.out.println(point);
    System.out.println("catch error:"+ex.getMessage());
}

当服务抛出错误时,该方法将捕获并解决它。 在我的服务中,我创建了一个 ArithmeticException,然后运行应用程序,打印结果是:

execution(TeacherInfo com.zhuyiren.service.TeacherService.getTeacherInfo())
catch error:/ by zero

当然,以上配置依赖于xml,也可以通过Annotation@Aspect,@Pointcut,@AfterThrowing来实现。

【讨论】:

    【解决方案2】:

    您可以定义一个方面。 使用基于 Java 的配置将如下所示:

    @Aspect
    public class ExceptionHandler {
    
        @AfterThrowing(pointcut="execution(* your.base.package..*.*(..))", throwing="ex")
        public void handleError(Exception ex) {
            //handling the exception
         }
    }
    

    如果需要注入bean,添加@Component注解:

    @Aspect
    @Component
    public class ExceptionHandler {
    
        @Autowired
        private NotificationService notificationService;
    
        @AfterThrowing(pointcut="execution(* your.base.package..*.*(..))", throwing="ex")
        public void handleError(Exception ex) {
            notificationService.sendMessage(ex.getMessage());
         }
    }
    

    【讨论】:

    猜你喜欢
    • 2018-09-02
    • 2018-10-15
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 2021-04-07
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多