Share on Google+Share on Google+

Spring AOP After Throws Advice

In this tutorial you will learn about Spring After Throws Advice which executed when a method throws an exception

Throws Advice Example

This advice is executed when a method throws an exception. To implement throws advice you need to implement ThrowsAdvice Interface of import org.springframework.aop.ThrowsAdvice package.


public interface SimpleInterface {
	void simpleMethod();


public class SimpleInterfaceImpl implements SimpleInterface {
	public void simpleMethod() {
		System.out.println(" ******* This is SimpleMethod() of Class "
				+ this.getClass().getName() + " ******* ");
		throw new RuntimeException();


import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.ThrowsAdvice;

public class AfterThrowingAdviceExample implements ThrowsAdvice {
	public void afterThrowing(RuntimeException runtimeException) {
		System.out.println(" ******* Inside Throws Advice ******* ");



import org.springframework.context.ApplicationContext;

public class AfterThrowingAdviceTest {
	public static void main(String[] args) {

		ApplicationContext applicationContext = new FileSystemXmlApplicationContext(

		SimpleInterface simpleInterface = (SimpleInterface) applicationContext
		try {
		} catch (Exception e) {




<?xml version="1.0" encoding="UTF-8"?>

    <!-- Bean configuration -->
    <bean id="proxyBean"
        <property name="proxyInterfaces">
        <property name="target">
            <ref local="beanTarget" />
        <property name="interceptorNames">

    <!-- Bean Classes -->
    <bean id="beanTarget" class="" />

    <!-- Advisor pointcut definition for before advice -->
    <bean id="theTracingThrowsAdvisor"
        <property name="advice">
            <ref local="theTracingThrowsAdvice" />
        <property name="pattern">

    <!-- Advice classes -->
    <bean id="theTracingThrowsAdvice"
        class="" />


When you run this application it will display message as shown below:

******* This is SimpleMethod() of Class *******
******* Inside Throws Advice *******

Download this example code



Posted on: September 3, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.