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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page