ABAP Unit: Test an Exception’s PREVIOUS Cause

To test an exception’s previous cause in ABAP Unit, catch the expected wrapper, assert that previous is bound, and check its exception type. Also call CL_ABAP_UNIT_ASSERT=>FAIL immediately after the tested call so that returning without an exception cannot pass unnoticed.
Define what the wrapper must preserve
A caller may need an application exception while diagnostics still need the technical cause. Passing the caught object through the constructor parameter previous preserves that link. Callers can inspect the public, read-only attribute inherited from CX_ROOT. See [SAP’s explanation of exception chaining](https://learning.sap.com/courses/deepening-your-abap-programming-knowledge/working-with-exception-classes_acd9568c-be4e-445a-a454-14c6f2cfcd2e).
Here the contract is precise: dividing a nonzero total by zero must raise lcx_calculation, with a CX_SY_ZERODIVIDE object as its immediate cause. The pattern applies to both Standard ABAP and ABAP Cloud.
Add a complete test with a controlled failure
Paste these local classes into the Test Classes tab of a global class in ADT. Activate the class and choose Run As → ABAP Unit Test. The helper and exception are defined here, so no database tables, demo data, or test-double framework are required.
The sample deliberately performs division by zero inside a handler. SAP documents this catchable exception in its [exceptions reference examples](https://github.com/SAP-samples/abap-cheat-sheets/blob/main/27_Exceptions.md).
CLASS lcx_calculation DEFINITION
INHERITING FROM cx_static_check FINAL.
ENDCLASS.
CLASS lcx_calculation IMPLEMENTATION.
ENDCLASS.
CLASS lcl_ratio DEFINITION FINAL.
PUBLIC SECTION.
CLASS-METHODS calculate
IMPORTING total TYPE i
count TYPE i
RETURNING VALUE(result) TYPE decfloat34
RAISING lcx_calculation.
ENDCLASS.
CLASS lcl_ratio IMPLEMENTATION.
METHOD calculate.
TRY.
result = total / count.
CATCH cx_sy_zerodivide INTO DATA(cause).
RAISE EXCEPTION TYPE lcx_calculation
EXPORTING previous = cause.
ENDTRY.
ENDMETHOD.
ENDCLASS.
CLASS ltc_exception_chain DEFINITION FINAL
FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
PRIVATE SECTION.
METHODS keeps_zero_divide FOR TESTING.
ENDCLASS.
CLASS ltc_exception_chain IMPLEMENTATION.
METHOD keeps_zero_divide.
DATA result TYPE decfloat34.
TRY.
result = lcl_ratio=>calculate(
total = 10
count = 0 ).
cl_abap_unit_assert=>fail(
msg = 'Expected LCX_CALCULATION' ).
CATCH lcx_calculation INTO DATA(error).
cl_abap_unit_assert=>assert_bound(
act = error->previous
msg = 'Original cause was lost' ).
IF error->previous IS INSTANCE OF cx_sy_zerodivide.
RETURN.
ENDIF.
cl_abap_unit_assert=>fail(
msg = 'Expected CX_SY_ZERODIVIDE as immediate cause' ).
ENDTRY.
ENDMETHOD.
ENDCLASS.
Derive the expected result
With total = 10 and count = 0, division raises CX_SY_ZERODIVIDE. The helper catches that object as cause, then raises lcx_calculation with the same object in previous.
The test therefore skips the first FAIL call and enters its specific CATCH block. The reference is bound, its dynamic type matches, and RETURN ends the test successfully. The expected outcome is one passing test; there is no console output.
The assertions follow SAP’s documented [ABAP Unit assertion methods](https://github.com/SAP-samples/abap-cheat-sheets/blob/main/14_ABAP_Unit_Tests.md).
Check that regressions turn the test red
These temporary edits show which behavior each check protects. Restore the original implementation after trying them.
CX_SY_ZERODIVIDE belongs to the dynamic-check exception category. Because calculate declares only lcx_calculation, removing its handler leaves the original exception undeclared at the method boundary. ABAP raises CX_SY_NO_HANDLER with the original object in previous; the test’s CATCH lcx_calculation does not catch it. See [SAP’s exception-category rules](https://github.com/SAP-samples/abap-cheat-sheets/blob/main/27_Exceptions.md#exception-categories).
| Temporary change | Expected test outcome |
|---|---|
Remove EXPORTING previous = cause | ASSERT_BOUND fails because the cause is missing. |
Replace the division and handler with result = 0 | The first FAIL reports the missing wrapper. |
Remove the helper’s exception handler, leaving result = total / count | CX_SY_NO_HANDLER reaches ABAP Unit, retaining CX_SY_ZERODIVIDE as its previous cause; the test does not pass. |
Limits and when to skip the chain assertion
Catch the expected wrapper specifically. A broad CATCH cx_root can accept unrelated failures and weaken the test. Avoid comparing translated exception text when the contract concerns the cause.
IS INSTANCE OF accepts the named type and its subclasses; it does not prove object identity. This example checks only the immediate cause, not every link in a longer chain. See SAP’s [object reference type checks](https://github.com/SAP-samples/abap-cheat-sheets/blob/main/04_ABAP_Object_Orientation.md?plain=1).
Do not assert a technical cause when the public contract intentionally leaves it unspecified. Such a test would couple callers to an implementation detail. For a method expected to succeed, let unexpected exceptions fail the test rather than catching them as an expected outcome.
Sources: SAP Learning: Working with Exception Classes · SAP ABAP Cheat Sheets: ABAP Unit Tests · SAP ABAP Cheat Sheets: Exceptions and Runtime Errors · SAP ABAP Cheat Sheets: Object Orientation