Call @afterTrowing advice from catch block to print exception thrown

We use Spring AOP with Log4j to register with our application. I implemented @Before, @After, @AfterTrowing advice in the app. But I ran into the following problem:

When any exception hits the catch block, it does not call the @AfterTrowing tip to print the trace of the error stack.

public void create() throws Exception
{
    try
    {
        throw new NullPointerException();
    }
    catch(NullPointerException ex)
    {
        // want to call any advice for printing ex.printStackTrace();           
    }

}

I want to print an "error stack trace" for an exception caught in a catch block. It means that every exception occurs in the try block and is caught by catch, then some tips should be called to print error details.

Thanks in advance!

+3
source share
3 answers

Spring AOP , , , . , AspectJ, ( pointcut handler()).

+1

. Spring . , - .

, @AfterThrowing, , - . , . . , .

0

spring . ,

public void create() throws Exception
{
    try
    {
        throw new NullPointerException();
    }
    catch(NullPointerException ex)
    {
        // want to call any advice for printing ex.printStackTrace();           
    }

}

try{}catch() . Aspect . try catch . .

class Abc{
public void create() throws Exception
{
    throw new NullPointerException();
}
}

public static void main(String[] args) {
    try{
        abc_obj.abc();
    }catch(NullPointerException ex){
        System.out.println("NUllpointer");
    }
}
0

All Articles