Handle an exception that occurs inside an aspect-proxied component call.
← Design Patterns and Idioms · Ref: Q1117
Exceptions propagate through aspects unchanged:
calc as Calculator! try result <- calc.divide(0) stdout.println(`100 / 0 = ${result}`) catch -> ex as Exception stdout.println(`caught: ${ex.reason()}`)
A silent aspect does not catch or modify exceptions -- they pass straight through. In the example the component throws Exception("zero divisor") for a divisor of 0 and the caller's try/catch handles it exactly as if no aspect were present.
See Q1115 for basic aspect. See Q1116 for aspect with constructor.
Example
#!ek9 defines module qa.designpatterns.aspectexception defines component Calculator as abstract divide() as abstract -> divisor as Integer <- result as Integer? CheckedCalculator is Calculator override divide() -> divisor as Integer <- result as Integer? if divisor == 0 throw Exception("zero divisor") result: 100 / divisor defines class AuditAspect extends Aspect default operator ? defines application ExceptionApp register CheckedCalculator() as Calculator with aspect of AuditAspect() defines program AspectExceptionDemo() with application of ExceptionApp stdout <- Stdout() calc as Calculator! //Normal call through aspect result <- calc.divide(10) stdout.println(`100 / 10 = ${result}`) //An exception thrown by the component passes through the aspect to this try/catch try failed <- calc.divide(0) stdout.println(`100 / 0 = ${failed}`) catch -> ex as Exception stdout.println(`caught: ${ex.reason()}`)
Other ways to ask this
- I need to understand what happens when a proxied method throws through an aspect
- In Spring AOP exceptions propagate through advice. Show the EK9 aspect exception path
- Given a component that throws, verify the exception passes through the aspect to the caller
- Test exception propagation through an aspect proxy
Coming from another language?
Spring AOP: exceptions propagate through @Around advice unless caught. AspectJ: same propagation. EK9: same — exceptions pass through the aspect proxy transparently.
Keywords: aspect, error, exception, propagate, proxy, try catch