Not just with Java.
But if you compile using the AspectJ compiler or modify your classes using weaving AspectJ load time, then this is possible.
Definition of an aspect from Wikipedia :
aspect is part of a program that solves major problems
Aspects are often used to add, for example, transaction logic and security.
To demonstrate how an aspect can solve your problem, I created an example that catches all exceptions returned from a call to the integration level from within a method in classes marked with @Service annotation. In this case, classes that contain .integration. in the name of the package.
This is just an example. You can modify it to intercept RuntimeExceptions in other places. For example, in all methods inside a class that has Facade in its name and calls other methods. See the picture below for ideas:

(source: espenberntsen.net )
The orange arrows are my AspectJ pointpect, illustrated with the AJDT plugin in Eclipse. These are places where you can catch and AfterThrowing exceptions in the AfterThrowing board.
Here is a tip:
@AfterThrowing(value="serviceMethodAfterExpcetionFromIntegrationLayerPointcut()", throwing="e") public void serviceMethodAfterExceptionFromIntegrationLayer(JoinPoint joinPoint, RuntimeException e) { StringBuilder arguments = generateArgumentsString(joinPoint.getArgs()); logger.error("Error in service " + joinPoint.getSignature() + " with the arguments: " + arguments, e); }
My serviceMethodAfterExpcetionFromIntegrationLayerPointcut actuall consists of two other poincuts:
@Pointcut("call(* *..integration.*.*(..))") public void integrationLayerPointcut() {} @Pointcut("within(@org.springframework.stereotype.Service *)") public void serviceBean() {} @Pointcut("serviceBean() && integrationLayerPointcut()") public void serviceMethodAfterExpcetionFromIntegrationLayerPointcut() {}
@Service , pointcut finds all places in the classes marked with the @Service annotation that call the method in the class at the integration level. (See the orange arrows in the picture)
If you want to catch runtime exceptions in your test classes, you can change the pointcut to this:
@Pointcut("within(com.redpill.linpro.demo..*Test)") public void inDemoProjectTestClass() {}
Then the above tip will catch all exceptions in test classes that end in Test in the package ..demo.
More information about @AspectJ 's style here .
AspectJ integrates well with Spring. General information about AspectJ can be found here, and Spring AspectJ support here .