Declare Soft

The semantics of the declare soft statement have been refined in AspectJ 5 to only soften exceptions that are not already runtime exceptions. If the exception type specified in a declare soft statement is RuntimeException or a subtype of RuntimeException then a new XLint warning will be issued:

declare soft : SomeRuntimeException : execution(* *(..));

>> "SomeRuntimeException will not be softened as it is already a RuntimeException" [XLint:runtimeExceptionNotSoftened]

This XLint message can be controlled by setting the runtimeExceptionNotSoftened XLint parameter.

If the exception type specified in a declare soft statement is a super type of RuntimeException (such as Exception for example) then any checked exception thrown at a matched join point, where the exception is an instance of the softened exception, will be softened to an org.aspectj.lang.SoftException.

public aspect SoftenExample {
  declare soft : Exception : execution(* Foo.*(..));
}

class Foo {
  public static void main(String[] args) {
    Foo foo = new Foo();
    foo.foo();
    foo.bar();
  }

  void foo() throws Exception {
    throw new Exception();        // this will be converted to a SoftException
  }

  void bar() throws Exception {
    throw new RuntimeException();  // this will remain a RuntimeException
  }
}