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?

Was this cameo in Captain Marvel computer generated?

What exactly is the meaning of "fine wine"?

Short story about cities being connected by a conveyor belt

Short story about an infectious indestructible metal bar?

Should we avoid writing fiction about historical events without extensive research?

How to distinguish easily different soldier of ww2?

Is this a crown race?

How would an energy-based "projectile" blow up a spaceship?

Is it a Cyclops number? "Nobody" knows!

What is the orbit and expected lifetime of Crew Dragon trunk?

Is it appropriate to ask a former professor to order a library book for me through ILL?

How do you make a gun that shoots melee weapons and/or swords?

Insult for someone who "doesn't know anything"

Draw this image in the TIKZ package

Unfamiliar notation in Diabelli's "Duet in D" for piano

Are small insurances worth it?

Unidentified signals on FT8 frequencies

What is the purpose of a disclaimer like "this is not legal advice"?

A running toilet that stops itself

Will the concrete slab in a partially heated shed conduct a lot of heat to the unconditioned area?

How can I paste into a layer with a layer mask?

What can I do if someone tampers with my SSH public key?

How strong is the axiom of well-ordered choice?

How can I have x-axis ticks that show ticks scaled in powers of ten?



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
    2 days ago












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

    – Essex Boy
    2 days ago















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
    2 days ago












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

    – Essex Boy
    2 days ago













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 2 days ago









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
    2 days ago












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

    – Essex Boy
    2 days ago












  • 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
    2 days ago












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

    – Essex Boy
    2 days ago







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
2 days ago






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
2 days ago














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

– Essex Boy
2 days ago





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

– Essex Boy
2 days ago












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
    2 days ago











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

    – Essex Boy
    2 days ago










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
    2 days ago











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

    – Essex Boy
    2 days ago















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
    2 days ago











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

    – Essex Boy
    2 days ago













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 2 days ago

























answered 2 days ago









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
    2 days ago











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

    – Essex Boy
    2 days ago

















  • 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
    2 days ago











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

    – Essex Boy
    2 days ago
















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
2 days ago





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
2 days ago













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

– Essex Boy
2 days ago





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

– Essex Boy
2 days ago



















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

AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

Алба-Юлія

Захаров Федір Захарович