Can't catch exception from main in: after() throwing(Exception e) - AspectJ

Viewed 2278

I am trying to catch an exception that is thrown within my main in my Java class.

My main's code:

public static void main(String[] args){
    new something();
    throw new RuntimeException();
}

In my aspect, I have created both after() returning: execution(* main(*)) { advice} and after() throwing(Exception e): execution(* main(*)) { advice } to find out if an exception is thrown in the main or not, in order to do something different at each advice.

Note that inside the second one, I am printing in the output the e exception using:

System.out.println("Threw an exception: " + e + "Joinpoint: " + thisJoinPoint.getSignature().toString());

The problem is that even if I throw an exception in main, and I can see from the output that the pointcut that is matched is the second one (Ouput: Threw an exception: java.lang.RuntimeExceptionJoinpoint: void main(String[]) ), I still get this error in my output:

Exception in thread "main" java.lang.RuntimeException
    at main(C.java:24)

So, as I can understand, I haven't catched the exception, I just identified that an exception happened in main.

Is there a way that I could catch this exception without having to use around() ?

1 Answers
Related