I have a huge Java application. I want to intercept all Java Exceptions adn send them by e-mail. I can't add everywhere code for sending the code via try-catch so is it possible to use for example Aspect to intercept the exception into low level classes and get the exception content?
Or is there some way to override some internal Java Class and get the exception payload?
What is possible?
解决方案
You can use the @AfterThrowing advice of spring-aop.
@Aspect
@Component
public class MailExceptionAspect {
@AfterThrowing(value="execution(* com.example..*.*(..))", throwing="ex" )
public void mailAfterThrowing(Throwable ex) {
// do something to send an email
}
}
This will intercept all exceptions, that are not handled, in the package com.example. Beware, that exceptions that are handled (caught) in the application, can not be intercepted.
Another solution would be to use the logging framework of the application. Many frameworks, like logback, log4j provide builtin configurations that can send logs by email.