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!
  result <- calc.divide(10)

A silent aspect does not catch or modify exceptions -- they pass straight through. The caller's try/catch handles exceptions as if no aspect were present.

See Q1115 for basic aspect. See Q1116 for aspect with constructor.

Example

defines module qa.designpatterns.aspectexception

  defines component

    Calculator as abstract
      divide() as abstract
        -> divisor as Integer
        <- result as Integer?

    SafeCalculator is Calculator
      override divide()
        -> divisor as Integer
        <- result as Integer: 100 / divisor

  defines class

    AuditAspect extends Aspect
      default operator ?

  defines application

    ExceptionApp
      register SafeCalculator() 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}`)
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: proxy, aspect, propagate, error, exception, try catch