why does my aspect code not run when exception is thrown?2019 Community Moderator ElectionHow do you assert that a certain exception is thrown in JUnit 4 tests?Why does Java have transient fields?Spring, Aspect J, multiple AfterThrowing advice applied to the same pointcutIntelliJ inspection gives “Cannot resolve symbol” but still compiles codeSpring Aspect not executed when defined in other JARWhy does this code using random strings print “hello world”?Why is executing Java code in comments with certain Unicode characters allowed?AspectJ and Spring: Exclude @After method execution when method throws an exceptionHow to log exception details in Spring AOP with try with empty catch block?java exception - why does it catch?

Does the in-code argument passing conventions used on PDP-11's have a name?

Why can't we use freedom of speech and expression to incite people to rebel against government in India?

Replacing tantalum capacitor with ceramic capacitor for Op Amps

What is "desert glass" and what does it do to the PCs?

Why are special aircraft used for the carriers in the United States Navy?

How do we objectively assess if a dialogue sounds unnatural or cringy?

Affine transformation of circular arc in 3D

Quitting employee has privileged access to critical information

What is Tony Stark injecting into himself in Iron Man 3?

Is being socially reclusive okay for a graduate student?

Called into a meeting and told we are being made redundant (laid off) and "not to share outside". Can I tell my partner?

School performs periodic password audits. Is my password compromised?

Paper published similar to PhD thesis

Has a sovereign Communist government ever run, and conceded loss, on a fair election?

Align equations with text before one of them

Is there a math expression equivalent to the conditional ternary operator?

Short story about an infectious indestructible metal bar?

Is there a frame of reference in which I was born before I was conceived?

Computing the volume of a simplex-like object with constraints

What's the best tool for cutting holes into duct work?

What is the meaning of option 'by' in TikZ Intersections

Why do phishing e-mails use faked e-mail addresses instead of the real one?

What is better: yes / no radio, or simple checkbox?

Naming Characters after Friends/Family



why does my aspect code not run when exception is thrown?



2019 Community Moderator ElectionHow do you assert that a certain exception is thrown in JUnit 4 tests?Why does Java have transient fields?Spring, Aspect J, multiple AfterThrowing advice applied to the same pointcutIntelliJ inspection gives “Cannot resolve symbol” but still compiles codeSpring Aspect not executed when defined in other JARWhy does this code using random strings print “hello world”?Why is executing Java code in comments with certain Unicode characters allowed?AspectJ and Spring: Exclude @After method execution when method throws an exceptionHow to log exception details in Spring AOP with try with empty catch block?java exception - why does it catch?










1















In my spring project I've added two dependencies:



<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>


and then I've created a class:



package com.my.company.package.handling;

@Aspect
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())




Now in some other class (stored in package: com.my.company.package.someOtherPackage) I'm throwing exception:



throw new IOException("here comes error");


but then I don't see the printout from my aspect method in the console. What am I missing here?










share|improve this question

















  • 1





    isn't there missing a .* for the method name? I don't use AspectJ but documentation states: "execution of any method defined in the service package: execution(* com.xyz.service.*.*(..))" (assuming that your someOtherPackage is a class), or: " execution of any method defined in the service package or a sub-package: execution(* com.xyz.service..*.*(..))"

    – Carlos Heuberger
    yesterday












  • @CarlosHeuberger just realised the same thing, I've updated my answer.

    – Essex Boy
    yesterday















1















In my spring project I've added two dependencies:



<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>


and then I've created a class:



package com.my.company.package.handling;

@Aspect
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())




Now in some other class (stored in package: com.my.company.package.someOtherPackage) I'm throwing exception:



throw new IOException("here comes error");


but then I don't see the printout from my aspect method in the console. What am I missing here?










share|improve this question

















  • 1





    isn't there missing a .* for the method name? I don't use AspectJ but documentation states: "execution of any method defined in the service package: execution(* com.xyz.service.*.*(..))" (assuming that your someOtherPackage is a class), or: " execution of any method defined in the service package or a sub-package: execution(* com.xyz.service..*.*(..))"

    – Carlos Heuberger
    yesterday












  • @CarlosHeuberger just realised the same thing, I've updated my answer.

    – Essex Boy
    yesterday













1












1








1


1






In my spring project I've added two dependencies:



<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>


and then I've created a class:



package com.my.company.package.handling;

@Aspect
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())




Now in some other class (stored in package: com.my.company.package.someOtherPackage) I'm throwing exception:



throw new IOException("here comes error");


but then I don't see the printout from my aspect method in the console. What am I missing here?










share|improve this question














In my spring project I've added two dependencies:



<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>


and then I've created a class:



package com.my.company.package.handling;

@Aspect
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())




Now in some other class (stored in package: com.my.company.package.someOtherPackage) I'm throwing exception:



throw new IOException("here comes error");


but then I don't see the printout from my aspect method in the console. What am I missing here?







java spring aspectj






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked yesterday









randomuser1randomuser1

1,07431539




1,07431539







  • 1





    isn't there missing a .* for the method name? I don't use AspectJ but documentation states: "execution of any method defined in the service package: execution(* com.xyz.service.*.*(..))" (assuming that your someOtherPackage is a class), or: " execution of any method defined in the service package or a sub-package: execution(* com.xyz.service..*.*(..))"

    – Carlos Heuberger
    yesterday












  • @CarlosHeuberger just realised the same thing, I've updated my answer.

    – Essex Boy
    yesterday












  • 1





    isn't there missing a .* for the method name? I don't use AspectJ but documentation states: "execution of any method defined in the service package: execution(* com.xyz.service.*.*(..))" (assuming that your someOtherPackage is a class), or: " execution of any method defined in the service package or a sub-package: execution(* com.xyz.service..*.*(..))"

    – Carlos Heuberger
    yesterday












  • @CarlosHeuberger just realised the same thing, I've updated my answer.

    – Essex Boy
    yesterday







1




1





isn't there missing a .* for the method name? I don't use AspectJ but documentation states: "execution of any method defined in the service package: execution(* com.xyz.service.*.*(..))" (assuming that your someOtherPackage is a class), or: " execution of any method defined in the service package or a sub-package: execution(* com.xyz.service..*.*(..))"

– Carlos Heuberger
yesterday






isn't there missing a .* for the method name? I don't use AspectJ but documentation states: "execution of any method defined in the service package: execution(* com.xyz.service.*.*(..))" (assuming that your someOtherPackage is a class), or: " execution of any method defined in the service package or a sub-package: execution(* com.xyz.service..*.*(..))"

– Carlos Heuberger
yesterday














@CarlosHeuberger just realised the same thing, I've updated my answer.

– Essex Boy
yesterday





@CarlosHeuberger just realised the same thing, I've updated my answer.

– Essex Boy
yesterday












1 Answer
1






active

oldest

votes


















0














Assuming everything else is correct you need a @Component annotation also, you also need another * in the execution string for any class.



@Aspect
@Component
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())




Here's a working example






share|improve this answer

























  • Thanks, however when I used it - I'm not seeing the message from logAfterThrowing. I tried to invoke it in my code by throwing e.g. throw new ParseException("msg", 0) or throw new NullPointerException("msg"); but message from Aspect does not show up

    – randomuser1
    yesterday











  • @randomuser1 it works for me in the test, something else wrong?

    – Essex Boy
    yesterday










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023224%2fwhy-does-my-aspect-code-not-run-when-exception-is-thrown%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Assuming everything else is correct you need a @Component annotation also, you also need another * in the execution string for any class.



@Aspect
@Component
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())




Here's a working example






share|improve this answer

























  • Thanks, however when I used it - I'm not seeing the message from logAfterThrowing. I tried to invoke it in my code by throwing e.g. throw new ParseException("msg", 0) or throw new NullPointerException("msg"); but message from Aspect does not show up

    – randomuser1
    yesterday











  • @randomuser1 it works for me in the test, something else wrong?

    – Essex Boy
    yesterday















0














Assuming everything else is correct you need a @Component annotation also, you also need another * in the execution string for any class.



@Aspect
@Component
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())




Here's a working example






share|improve this answer

























  • Thanks, however when I used it - I'm not seeing the message from logAfterThrowing. I tried to invoke it in my code by throwing e.g. throw new ParseException("msg", 0) or throw new NullPointerException("msg"); but message from Aspect does not show up

    – randomuser1
    yesterday











  • @randomuser1 it works for me in the test, something else wrong?

    – Essex Boy
    yesterday













0












0








0







Assuming everything else is correct you need a @Component annotation also, you also need another * in the execution string for any class.



@Aspect
@Component
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())




Here's a working example






share|improve this answer















Assuming everything else is correct you need a @Component annotation also, you also need another * in the execution string for any class.



@Aspect
@Component
public class MyAspect
@AfterThrowing(pointcut = "execution(* com.my.company.package.*.*(..))", throwing = "ex")
public void logAfterThrowing(Exception ex)
System.out.println("exception "+ex.getLocalizedMessage())




Here's a working example







share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









Essex BoyEssex Boy

4,6781816




4,6781816












  • Thanks, however when I used it - I'm not seeing the message from logAfterThrowing. I tried to invoke it in my code by throwing e.g. throw new ParseException("msg", 0) or throw new NullPointerException("msg"); but message from Aspect does not show up

    – randomuser1
    yesterday











  • @randomuser1 it works for me in the test, something else wrong?

    – Essex Boy
    yesterday

















  • Thanks, however when I used it - I'm not seeing the message from logAfterThrowing. I tried to invoke it in my code by throwing e.g. throw new ParseException("msg", 0) or throw new NullPointerException("msg"); but message from Aspect does not show up

    – randomuser1
    yesterday











  • @randomuser1 it works for me in the test, something else wrong?

    – Essex Boy
    yesterday
















Thanks, however when I used it - I'm not seeing the message from logAfterThrowing. I tried to invoke it in my code by throwing e.g. throw new ParseException("msg", 0) or throw new NullPointerException("msg"); but message from Aspect does not show up

– randomuser1
yesterday





Thanks, however when I used it - I'm not seeing the message from logAfterThrowing. I tried to invoke it in my code by throwing e.g. throw new ParseException("msg", 0) or throw new NullPointerException("msg"); but message from Aspect does not show up

– randomuser1
yesterday













@randomuser1 it works for me in the test, something else wrong?

– Essex Boy
yesterday





@randomuser1 it works for me in the test, something else wrong?

– Essex Boy
yesterday



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55023224%2fwhy-does-my-aspect-code-not-run-when-exception-is-thrown%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme