2V0-72.22PSE · Question #16
Which statement describes the @AfterReturning advice type? (Choose the best answer.)
The correct answer is A. The advice is invoked only if the method returns successfully but not if it throws an exception. @AfterReturning fires exclusively on a successful method return - if the method throws an exception, the advice is skipped entirely, making option A the precise definition. Why the distractors are wrong: B (truncated, but describes running "even if" something) points to @After…
Question
Which statement describes the @AfterReturning advice type? (Choose the best answer.)
Options
- AThe advice is invoked only if the method returns successfully but not if it throws an exception.
- BThe @AfterReturning advice allows behavior to be added after a method returns even if it
- CThe advice has complete control over the method invocation; it could even prevent the method
- DTypically used to prevent any exception, thrown by the advised method, from propagating up
How the community answered
(22 responses)- A86% (19)
- B5% (1)
- C9% (2)
Explanation
@AfterReturning fires exclusively on a successful method return - if the method throws an exception, the advice is skipped entirely, making option A the precise definition.
Why the distractors are wrong:
- B (truncated, but describes running "even if" something) points to
@After(a.k.a. "after finally"), which runs regardless of whether the method succeeds or throws - that's the key difference from@AfterReturning. - C describes
@Aroundadvice, which wraps the entire method invocation and can even prevent the target method from executing at all. - D describes
@AfterThrowing, which is triggered specifically by exceptions propagating out of the advised method.
Memory tip: Think of @AfterReturning as a success-only callback - it only "celebrates" when the method comes home safely. If anything goes wrong (exception), the party is cancelled. Contrast this with @After (the "always runs" finally block) and @AfterThrowing (the "only runs on disaster" handler).
Topics
Community Discussion
No community discussion yet for this question.