Spring Boot - AOP After Throwing Advice

Last Updated : 14 Mar, 2026

In Spring AOP, After Throwing Advice is used to execute code when a method throws an exception. It is defined using the @AfterThrowing annotation. This advice is helpful for handling errors, logging exceptions, or performing cleanup operations when something goes wrong during method execution.

  • @AfterThrowing advice runs only when the target method throws an exception.
  • It is commonly used for exception logging and error handling.
  • It helps manage errors in a centralized way without modifying business logic.

Syntax:

@Aspect
public class AfterThrowingExample {
@AfterThrowing("execution(* com.xyz.dao.*.*(..))")
public void doRecoveryActions() {
}
}

Steps to Implement After Throwing Advice

Let’s understand the steps required to implement @AfterThrowing advice in Spring AOP, which executes when a target method throws an exception.

Step 1: Create a Spring Boot Project

Open Spring Initializr and provide the following details:

  • Group: com.afterthrowing
  • Artifact: aop-afterthrowing-example
  • Dependencies: Spring Web

Download the project and extract the ZIP file.

Step 2: Add Dependencies in pom.xml

XML
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

 Step 3: Create DAO Class

Create package com.afterthrowing.dao and add MockDAO.java.

Java
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Repository;

// Annotation
@Repository

// Class
public class MockDAO {

    // Not actually accessing a database,
    // just giving a mock data
    public List<String> getEmployees(boolean exception)
    {
        if (exception) {
            throw new RuntimeException("You asked for it");
        }

        return Arrays.asList("Mary", "Jerome", "Vyom");
    }
}

  Step 4: Create Controller Class

Create package com.afterthrowing.controller and add DefaultWebController.java.

Java
import com.geeksforgeeks.springbootaopafterthrowing.dao.MockDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

// Annotation
@Controller

// Class
public class DefaultWebController {

    // Class data member
    private final MockDAO mockDAO;

    // Annotation
    @Autowired public DefaultWebController(MockDAO mockDAO)
    {
        // This keyword refers to current instance itself
        this.mockDAO = mockDAO;
    }

    // Annotation
    @GetMapping("/")

    // Method
    public String homePage(Model model)
    {
        model.addAttribute("list",
                           mockDAO.getEmployees(false));
        return "index";
    }
}

Step 5: Create html page

After that, we will add the index.html page under resources.templates package.

HTML
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>Homepage</title>
</head>
<body>
<table>
    <thead>
    <tr>
        <th>Name:</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="emp : ${list}">
        <td th:text="${emp}"></td>
    </tr>
    </tbody>
</table>
</body>
</html>

 
We basically access that model attribute that was passed by the DefaultWebController, as a list was passed on, we just iterate over each entry and print it out.

Step 6: Create Aspect Class

Create package com.afterthrowing.aspect and add LoggingAspect.java.

Java
package com.afterthrowing.aspect;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

    @AfterThrowing(
        "execution(public java.util.List com.afterthrowing.dao.MockDAO.getEmployees(..))")

    public void afterThrowingExceptionAdvice(){

        System.out.println(
          "\n =====>> Exception was thrown in DAO method\n");
    }
}

Step 7: Run the Application

Run the project using:

mvn spring-boot:run

Open the browser:

http://localhost:8080/

Output
Terminal Output

 

Comment