Created
October 27, 2024 11:35
-
-
Save muhdkhokhar/bdca426c78c0881c53e1a3b02554e645 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.aspectj.lang.annotation.Aspect; | |
import org.aspectj.lang.annotation.AfterThrowing; | |
import org.aspectj.lang.annotation.Pointcut; | |
import org.springframework.stereotype.Component; | |
@Aspect | |
@Component | |
public class GlobalExceptionInterceptor { | |
// Define a pointcut to intercept all methods in your application's packages | |
@Pointcut("execution(* com.yourpackage..*(..))") | |
public void applicationPackagePointcut() { | |
// Pointcut for all methods in com.yourpackage | |
} | |
// After throwing advice to capture exceptions | |
@AfterThrowing(pointcut = "applicationPackagePointcut()", throwing = "ex") | |
public void logException(Exception ex) { | |
System.err.println("Exception captured: " + ex.getClass().getName() + " - " + ex.getMessage()); | |
ex.printStackTrace(); | |
// Additional handling such as logging to a file or sending alerts | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment