Pages Navigation Menu

Coding is much easier than you think

Spring AOP – AfterThrows Advice

Spring AOP – AfterThrows Advice


Requirement : In case a particular method call throws an exception, a log needs to be printed.
** UPDATE: Spring Complete tutorial now available here.

Step 1 :

Changes in IteratorImpl class to mock an exception being thrown. No change in IteratorImpl interface

File :

package com.simpleCodeStuffs.aop;

public interface Iterator {
void goPrevious();
void goNext();

File :

package com.simpleCodeStuffs.aop;

public class IteratorImpl implements Iterator{
String flowName;
public void goNext() {
System.out.println("goNext Method Called on flow - "+flowName);
throw new RuntimeException();
public void goPrevious(){
System.out.println("goPrevious Method called on flow - "+flowName);

public String getFlowName() {
return flowName;

public void setFlowName(String flowName) {
this.flowName = flowName;


Step 2 :

Create a new class AopExampleAfterThrows to implement the new functionality

File :

package com.simpleCodeStuffs.aop;
import org.springframework.aop.ThrowsAdvice;
public class AopExampleAfterThrows implements ThrowsAdvice {
public void afterThrowing(RuntimeException runtimeException) {
System.out.println("Logging step : Exception thrown by the method ");


Step 3 :

Changes in the aopBeans.xml file. Similar to the Before Advice and After returns Advice, create the advisor and advice beans
File : aopBeans.xml

<?xml version="1.0" encoding="UTF-8"?>
	<!-- Bean Classes -->
	<bean id="beanTarget" class="com.simpleCodeStuffs.aop.IteratorImpl">
		<property name="flowName" value="existing code" />

	<!-- Bean configuration -->
<bean id="businesslogicbean" class="org.springframework.aop.framework.ProxyFactoryBean">
	<property name="proxyInterfaces">
	<property name="target">
		<ref local="beanTarget" />
	<property name="interceptorNames">

<bean id="theAfterThrowingAdvisor"
	<property name="advice">
		<ref local="theAfterThrowingAdvice" />
	<property name="pattern">

<bean id="theAfterThrowingAdvice" class="com.simpleCodeStuffs.aop.AopExampleAfterThrows" />


Step 4 :

No major change in the Main class. Simply place a call to the method that throws a mock exception.
File :

package com.simpleCodeStuffs.aop;

import org.springframework.context.ApplicationContext;

public class SpringAopMain {

public static void main(String[] args) {
// Read the configuration file
ApplicationContext context =
new ClassPathXmlApplicationContext("aopBeans.xml");
// Instantiate an object
Iterator IteratorInterface = (Iterator) context

// Execute the public method of the bean


Step 5 :

Run the Main class. The output is as follows



Please note that the exception shown in the above output is to show the working of the AfterThrows Advice once an exception is thrown. Handling this exception is simple enough. Read on to the next tutorial, wherein in aroundAdvice, this exception has been handled properly.



About Mohaideen Jamil

I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

%d bloggers like this: